<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		
	<span class="different-posts different-posts_earlier">📖 <a href="/page/36">earlier posts</a> 📖</span>




	<article>
	
		<h1><a href="/2017/10/12/Linux安全/1. CSRF攻击防范/">CSRF攻击防范</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux安全/">Linux安全</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;CSRF攻击必须依次完成以下两个条件： </p>
<ol>
<li>登录受信任网站A，并在本地生成Cookie。 </li>
<li>在不登出A的情况下，访问危险网站B。</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;关闭浏览器不能结束一个会话，但大多数人都会错误的认为关闭浏览器就等于退出登录/结束会话了</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;CSRF攻击形成的原因： </p>
<ol>
<li>网站违反了HTTP协议，使用GET请求更新资源(非常危险). </li>
<li>使用无校验的表单</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;CSRF攻击是源于WEB的隐式身份验证机制！(比如你在A网站登录后,在同一个浏览器的tab页打开B网站网页, 而B网站网页有一些脚本链接到A网站并偷偷的发送请求更新你账号的信息.如果没有防CSRF攻击,这样是可以实现的.) </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;WEB的身份验证机制虽然可以保证一个请求是来自于某个用户的浏览器，但却无法保证该请求是用户批准发送的！</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;CSRF防范方式：客户端页面增加伪随机数。每次需要修改该用户的数据库信息时必须带上该随机数,否则不受理.</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解决办法: 在Form表单加一个hidden field，里面是服务端生成的足够随机数的一个Token(恶意网站猜不到也无法获取到相同的Token), 然后使用一个拦截器interceptor来检查每一个非get请求, 看该token与服务器token是否一致,不一致的不受理该请求. </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以下是token 的工具管理类:</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div></pre></td><td class="code"><pre><div class="line">package com.xxx.xxx.util;</div><div class="line"></div><div class="line">import javax.servlet.http.HttpServletRequest;</div><div class="line">import javax.servlet.http.HttpSession;</div><div class="line">import java.util.UUID;</div><div class="line"></div><div class="line"></div><div class="line">/**</div><div class="line"> * CSRFtoken管理类</div><div class="line"> *</div><div class="line"> * @author 作者  yss</div><div class="line"> * @version 版本号 v1.0</div><div class="line"> */</div><div class="line">public final class CSRFTokenManager &#123;</div><div class="line"></div><div class="line"></div><div class="line">    /**</div><div class="line">     * 约定规范：表单提交时的token的input的name属性必须为该值才能获取到后台返回的token。</div><div class="line">     * 下面是使用EL表达式接收从后台返回的token参数</div><div class="line">     * 如: &lt;input <span class="built_in">type</span>=<span class="string">"hidden"</span> id=<span class="string">"CSRFToken"</span> value=<span class="string">"<span class="variable">$&#123;CSRFToken&#125;</span>"</span>&gt;</div><div class="line">     */</div><div class="line">    public static final String CSRF_PARAM_NAME = <span class="string">"CSRFToken"</span>;</div><div class="line"></div><div class="line">    /**</div><div class="line">     * 存放在session中的token名称(跟上面的name属性值不一定一样)</div><div class="line">     */</div><div class="line">    public static final String CSRF_TOKEN_FOR_SESSION_ATTR_NAME = CSRFTokenManager.class.getName() + <span class="string">".tokenval"</span>;</div><div class="line"></div><div class="line">    /**</div><div class="line">     *从session中获取token字符串</div><div class="line">     * @param session</div><div class="line">     * @<span class="built_in">return</span></div><div class="line">     */</div><div class="line">    public static String getTokenForSession(HttpSession session) &#123;</div><div class="line">        String token = null;</div><div class="line">        //保证session只存在一个token，避免多线程情况下产生冲突。</div><div class="line">        synchronized (session) &#123;</div><div class="line">            token = (String) session.getAttribute(CSRF_TOKEN_FOR_SESSION_ATTR_NAME);//尝试获取session中的token</div><div class="line">            <span class="keyword">if</span> (null == token) &#123;//如果session中没有token，就重新生成一个token</div><div class="line">                token = UUID.randomUUID().toString();</div><div class="line">                session.setAttribute(CSRF_TOKEN_FOR_SESSION_ATTR_NAME, token);</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">        <span class="built_in">return</span> token;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    /**</div><div class="line">     *获取到request中的token值。</div><div class="line">     * @param request</div><div class="line">     * @<span class="built_in">return</span></div><div class="line">     */</div><div class="line"></div><div class="line">    public static String getTokenFromRequest(HttpServletRequest request) &#123;</div><div class="line">        <span class="built_in">return</span> request.getParameter(CSRF_PARAM_NAME);</div><div class="line">    &#125;</div><div class="line">//构造器</div><div class="line">    private <span class="function"><span class="title">CSRFTokenManager</span></span>() &#123;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;后台返回token参数给页面:</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;String token = CSRFTokenManager.getTokenForSession(request.getSession());//uuid生成的随机token</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;modelAndView.addObject(CSRFTokenManager.CSRF_PARAM_NAME, token);//添加token参数 <input type="hidden" id="CSRFToken" value="${CSRFToken}">&lt;%–token–%&gt;</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编写代码要符合HTTP规范,不要使用GET请求更新资源 </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在非GET请求中带上token 参数(ajax请求也要),然后使用拦截器检查. </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;对于受到CSRF攻击使用的是抛自定义异常,然后使用springmvc全局异常进行处理</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div></pre></td><td class="code"><pre><div class="line">package com.xxx.xxx.interceptor;</div><div class="line"></div><div class="line">import com.xxx.xxx.exception.CSRFException;</div><div class="line">import com.xxx.xxx.util.CSRFTokenManager;</div><div class="line">import org.springframework.web.servlet.ModelAndView;</div><div class="line">import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;</div><div class="line"></div><div class="line">import javax.servlet.http.HttpServletRequest;</div><div class="line">import javax.servlet.http.HttpServletResponse;</div><div class="line"></div><div class="line">/**</div><div class="line"> * 对于未登录用户的请求进行拦截,确保用户已经登录,然后进行后续的网页请求</div><div class="line"> *</div><div class="line"> * @Author yss</div><div class="line"> * @Version 1.0</div><div class="line"> * @see</div><div class="line"> */</div><div class="line">public class CSRFInterceptor extends HandlerInterceptorAdapter &#123;</div><div class="line"></div><div class="line">    @Override</div><div class="line">    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception &#123;</div><div class="line">//        Enumeration parasm = request.getParameterNames();</div><div class="line">        <span class="keyword">if</span> (!<span class="string">"GET"</span>.equals(request.getMethod())) &#123;//非get请求</div><div class="line">            String CSRFToken = CSRFTokenManager.getTokenFromRequest(request);//页面传过来的csrf参数</div><div class="line">            <span class="keyword">if</span> (CSRFToken == null || !CSRFToken.equals(CSRFTokenManager.getTokenForSession(request.getSession()))) &#123;//token不对应</div><div class="line">                throw new CSRFException(<span class="string">"CSRF攻击"</span>);//抛异常后将会进入springmvc全局异常处理体系</div><div class="line">            &#125;</div><div class="line">        &#125;</div><div class="line">        <span class="built_in">return</span> <span class="literal">true</span>;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    @Override</div><div class="line">    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception &#123;</div><div class="line">        super.postHandle(request, response, handler, modelAndView);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在spring的配置文件中添加拦截器使其生效</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">&lt;mvc:interceptors&gt;</div><div class="line">    &lt;!-- 防止CSRF攻击的拦截器 --&gt;</div><div class="line">    &lt;mvc:interceptor&gt;</div><div class="line">        &lt;mvc:mapping path=<span class="string">"/**"</span>/&gt;</div><div class="line">        &lt;bean id=<span class="string">"CSRFInterceptor"</span> class=<span class="string">"com.xxx.xxx.interceptor.CSRFInterceptor"</span>&gt;&lt;/bean&gt;</div><div class="line">    &lt;/mvc:interceptor&gt;</div><div class="line">&lt;/mvc:interceptors&gt;</div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/jenkins/2.使用jenkins进行持续集成/">使用jenkins进行持续集成</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Jenkins/">Jenkins</a>
			</span>
		
	</div>

	

	
		<h2 id="安装JDK"><a href="#安装JDK" class="headerlink" title="安装JDK"></a>安装JDK</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;因为jenkins需要java环境，需要提前安装JDK。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下载JDK后解压</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># tar zxvf jdk-8u111-linux-x64.tar.gz</span></div><div class="line">[root@localhost ~]<span class="comment"># mv jdk1.8.0_111/ /usr/local/</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># vim /etc/profile.d/java.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;加入配置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">JAVA_HOME=/usr/<span class="built_in">local</span>/jdk1.8.0_111/</div><div class="line">JAVA_BIN=/usr/<span class="built_in">local</span>/jdk1.8.0_111/bin</div><div class="line">JRE_HOME=/usr/<span class="built_in">local</span>/jdk1.8.0_111/jre</div><div class="line">PATH=<span class="variable">$PATH</span>:/usr/<span class="built_in">local</span>/jdk1.8.0_111/bin:/usr/<span class="built_in">local</span>/jdk1.8.0_111/jre/bin</div><div class="line">CLASSPATH=/usr/<span class="built_in">local</span>/jdk1.8.0_111/jre/lib:/usr/<span class="built_in">local</span>/jdk1.8.0_111/lib:/usr/<span class="built_in">local</span>/jdk1.8.0_111/jre/lib/charsets.jar</div><div class="line"><span class="built_in">export</span> JAVA_HOME JAVA_BIN JRE_HOME PATH CLASSPATH</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后进入初始化环境变量</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># source /etc/profile.d/java.sh</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果JDK配置成功，下面的名利个可以看到java版本</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># java -version</span></div><div class="line">java version <span class="string">"1.8.0_111"</span></div><div class="line">Java(TM) SE Runtime Environment (build 1.8.0_111-b14)</div><div class="line">Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode)</div></pre></td></tr></table></figure>
<h2 id="下载安装jenkins"><a href="#下载安装jenkins" class="headerlink" title="下载安装jenkins"></a>下载安装jenkins</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先，从<a href="https://jenkins.io" target="_blank" rel="external">jenkins官方网站</a>下载最新的包。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;选择Red Hat/CentOS，可以根据官网提示安装</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/01.png?raw=true" alt="01"><figcaption class="figure__caption">01</figcaption></figure></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat/jenkins.repo</div><div class="line">rpm --import https://pkg.jenkins.io/redhat/jenkins.io.key</div><div class="line">yum install jenkins</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;也可以下载相应的rpm包进行安装</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># rpm -ivh jenkins-2.78-1.1.noarch.rpm </span></div><div class="line">警告：jenkins-2.78-1.1.noarch.rpm: 头V4 DSA/SHA1 Signature, 密钥 ID d50582e6: NOKEY</div><div class="line">准备中...                                                            (<span class="comment">################################# [100%]</span></div><div class="line">正在升级/安装...</div><div class="line">   1:jenkins-2.78-1.1                                                 <span class="comment">################################# [100%]</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;另外也可以下载war包，只需要运行命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># java -jar jenkins.war</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;jenkins就启动成功了！war包自带jetty服务器，剩下的工作全部在浏览器中进行。</p>
<h2 id="防火墙设置"><a href="#防火墙设置" class="headerlink" title="防火墙设置"></a>防火墙设置</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># systemctl stop firewalld</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;或者</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">firewall-cmd –zone=public –add-port=8080/tcp –permanent </div><div class="line">firewall-cmd –zone=public –add-service=http –permanent </div><div class="line">firewall-cmd –reload</div></pre></td></tr></table></figure>
<h2 id="启动jenkins服务"><a href="#启动jenkins服务" class="headerlink" title="启动jenkins服务"></a>启动jenkins服务</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动jenkins服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># /etc/init.d/jenkins start</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动失败</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># /etc/init.d/jenkins start</span></div><div class="line">Starting jenkins (via systemctl):  Job <span class="keyword">for</span> jenkins.service failed because the control process exited with error code. See <span class="string">"systemctl status jenkins.service"</span> and <span class="string">"journalctl -xe"</span> <span class="keyword">for</span> details.</div><div class="line">                                                           [失败]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;出错原因是jdk未安装或jenkins配置文件中没有找到jdk的路径</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;解决办法是安装jdk，修改jenkins启动脚本,配置jdk位置</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># vim /etc/init.d/jenkins</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;添加内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/usr/<span class="built_in">local</span>/jdk1.8.0_111/bin/java</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/02.png?raw=true" alt="02"><figcaption class="figure__caption">02</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果错误提示</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># /etc/init.d/jenkins start</span></div><div class="line">Starting jenkins (via systemctl):  Warning: jenkins.service changed on disk. Run <span class="string">'systemctl daemon-reload'</span> to reload units.</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用命令解决</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># systemctl daemon-reload</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再次启动jenkins</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># /etc/init.d/jenkins restart</span></div><div class="line">Restarting jenkins (via systemctl):                        [  确定  ]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看是否启动</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># netstat -natpu|grep 8080</span></div><div class="line">tcp6       0      0 :::8080                 :::*                    LISTEN      5041/java</div></pre></td></tr></table></figure>
<h2 id="访问jenkins"><a href="#访问jenkins" class="headerlink" title="访问jenkins"></a>访问jenkins</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在浏览器中输入 <a href="http://ip:8080" target="_blank" rel="external">http://ip:8080</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果安装完成并成功启动会有下图</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/03.png?raw=true" alt="03"><figcaption class="figure__caption">03</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;第一次启动jenkins时，出于安全考虑，jenkins会自动生成一个随机的密码。查看/var/lib/jenkins/secrets/initialAdminPassword这个文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># cat /var/lib/jenkins/secrets/initialAdminPassword</span></div><div class="line">2bdb5321eac84842b6e37a08758d3654</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;将密码复制出来，粘贴，继续进入配置页面</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/04.png?raw=true" alt="04"><figcaption class="figure__caption">04</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这里可以根据自己需求选择推荐插件或者自己选择插件安装。如果执行默认的安装，jenkins就自动配置好Maven、git等常用插件。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/05.png?raw=true" alt="05"><figcaption class="figure__caption">05</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;最后，创建一个admin用户，完成安装。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/06.png?raw=true" alt="06"><figcaption class="figure__caption">06</figcaption></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/07.png?raw=true" alt="07"><figcaption class="figure__caption">07</figcaption></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/08.png?raw=true" alt="08"><figcaption class="figure__caption">08</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用管理员账号登录Jenkins后，第一次使用前，需要在“系统管理”-&gt;“Global Tool Configuration”-&gt;“Maven”中新增一个Maven，直接输入一个名字，选中“自动安装”，Jenkins会自动下载并安装Maven</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/09.png?raw=true" alt="09"><figcaption class="figure__caption">09</figcaption></figure></p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/10.png?raw=true" alt="10"><figcaption class="figure__caption">10</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果不成功就在“管理插件”里边选择“可选插件”，安装<code>Maven Integration plugin</code>。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/11.png?raw=true" alt="11"><figcaption class="figure__caption">11</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后，在Jenkins首页选择“新建”，输入名字，选择“构建一个maven项目”</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/12.png?raw=true" alt="12"><figcaption class="figure__caption">12</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在配置页中，源码管理选择Git，填入地址</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/13.png?raw=true" alt="13"><figcaption class="figure__caption">13</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;默认使用master分支。如果需要口令，在Credentials中添加用户名/口令，或者使用SSH Key。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;构建触发器指定了触发一次构建的条件。推荐使用最简单的配置“Poll SCM”，它的意思是，定时检查版本库，发现有新的提交就触发构建。这种方式对git、SVN等所有版本管理系统都是通用的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在日程表中填入</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">* * * * *</div></pre></td></tr></table></figure>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/14.png?raw=true" alt="14"><figcaption class="figure__caption">14</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;表示每分钟检查一次。如果觉得太频繁，可以该成“每3分钟检查一次”</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">*/3 * * * *</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在“Build”中，默认的Root POM是<code>pom.xml</code>。如果<code>pom.xml</code>不在根目录下，就填入子目录，例如：<code>wxapi/pom.xml</code>。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在Goals and options中，填入需要执行的mvn命令：<code>clean package</code>，Jenkins将执行如下命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">mvn clean package</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;特殊参数也在这里填写，如<code>-DskipTests=true clean package</code>。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保存后，就可以执行自动化构建了。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;点击一个构建任务，可以在Console Output中看到控制台详细输出，便于出错排查</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E5%AE%89%E8%A3%85%E4%B8%8E%E4%B8%8B%E8%BD%BD%E5%BA%94%E7%94%A8%E4%BB%A3%E7%A0%81/15.png?raw=true" alt="15"><figcaption class="figure__caption">15</figcaption></figure></p>
<h2 id="如何部署"><a href="#如何部署" class="headerlink" title="如何部署"></a>如何部署</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果要部署构建好的war包，可以在Post Steps中填上shell命令，直接用脚本部署。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;另一种方式是创建另外一个构建项目，手动触发部署。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;无论用哪种方式，都是为了确保编译、部署是通过CI服务器完成的，而不是某台开发机器。</p>
<h2 id="如何创建Linux服务"><a href="#如何创建Linux服务" class="headerlink" title="如何创建Linux服务"></a>如何创建Linux服务</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;有了Jenkins，就可以在内网或者租用一台EC2服务器来搭建CI环境。因为不想每次登录到Linux去启动Jenkins，也不想写脚本来启动服务。推荐安装JDK后，配合supervisor，把Jenkins直接变成一个服务。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;可以在Linux上创建一个<code>ci</code>用户，然后，用supervisor启动并指定9001端口：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># /etc/supervisor/conf.d/ci.conf</span></div><div class="line"></div><div class="line">[program:ci]</div><div class="line"><span class="built_in">command</span>=java -jar /home/ci/jenkins.war --httpPort=9001</div><div class="line">user=ci</div><div class="line">autostart=<span class="literal">true</span></div><div class="line">autorestart=<span class="literal">true</span></div><div class="line">startsecs=30</div><div class="line">startretries=5</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Jenkins默认在当前用户的主目录下创建<code>.jenkins</code>目录，所有的配置文件、数据库都存放在里面，只需要备份这个目录就备份了整个CI配置。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这样，一个CI环境就搭建完毕。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/jenkins/1.jenkins是什么？/">jenkins是什么？</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Jenkins/">Jenkins</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Jenkins 是一个可扩展的持续集成引擎。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主要用于：</p>
<ul>
<li>持续、自动地构建/测试软件项目。</li>
<li>监控一些定时执行的任务。</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Jenkins拥有的特性包括：</p>
<ul>
<li>易于安装-只要把jenkins.war部署到servlet容器，不需要数据库支持。</li>
<li>易于配置-所有配置都是通过其提供的web界面实现。</li>
<li>集成RSS/E-mail通过RSS发布构建结果或当构建完成时通过e-mail通知。</li>
<li>生成JUnit/TestNG测试报告。</li>
<li>分布式构建支持Jenkins能够让多台计算机一起构建/测试。</li>
<li>文件识别:Jenkins能够跟踪哪次构建生成哪些jar，哪次构建使用哪个版本的jar等。</li>
<li>插件支持:支持扩展插件，你可以开发适合自己团队使用的工具。</li>
</ul>
<h2 id="Jenkins的由来"><a href="#Jenkins的由来" class="headerlink" title="Jenkins的由来"></a>Jenkins的由来</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;目前持续集成(CI)已成为当前许多软件开发团队在整个软件开发生命周期内侧重于保证代码质量的常见做法。它是一种实践，旨在缓和和稳固软件的构建过程。并且能够帮助您的开发团队应对如下挑战：</p>
<ul>
<li>软件构建自动化 ：配置完成后，CI系统会依照预先制定的时间表，或者针对某一特定事件，对目标软件进行构建。</li>
<li>构建可持续的自动化检查 ：CI系统能持续地获取新增或修改后签入的源代码，也就是说，当软件开发团队需要周期性的检查新增或修改后的代码时，CI系统会不断确认这些新代码是否破坏了原有软件的成功构建。这减少了开发者们在检查彼此相互依存的代码中变化情况需要花费的时间和精力(说直接一点也是钱啊，呵呵)。</li>
<li>构建可持续的自动化测试 ：构建检查的扩展部分，构建后执行预先制定的一套测试规则，完成后触发通知(Email,RSS等等)给相关的当事人。</li>
<li>生成后后续过程的自动化 :当自动化检查和测试成功完成，软件构建的周期中可能也需要一些额外的任务，诸如生成文档、打包软件、部署构件到一个运行环境或者软件仓库。这样，构件才能更迅速地提供给用户使用。</li>
</ul>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;部署一个CI系统需要的最低要求是，一个可获取的源代码的仓库，一个包含构建脚本的项目。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下图概括了CI系统的基本结构：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/jenkins%E6%98%AF%E4%BB%80%E4%B9%88/01.jpg?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;该系统的各个组成部分是按如下顺序来发挥作用的：</p>
<ol>
<li><p>开发者检入代码到源代码仓库。</p>
</li>
<li><p>CI系统会为每一个项目创建了一个单独的工作区。当预设或请求一次新的构建时，它将把源代码仓库的源码存放到对应的工作区。</p>
</li>
<li><p>CI系统会在对应的工作区内执行构建过程。</p>
</li>
<li><p>（配置如果存在）构建完成后，CI系统会在一个新的构件中执行定义的一套测试。完成后触发通知(Email,RSS等等)给相关的当事人。</p>
</li>
<li><p>（配置如果存在）如果构建成功，这个构件会被打包并转移到一个部署目标(如应用服务器)或存储为软件仓库中的一个新版本。软件仓库可以是CI系统的一部分，也可以是一个外部的仓库，诸如一个文件服务器或者像Java.net、 SourceForge之类的网站。</p>
</li>
<li><p>CI系统通常会根据请求发起相应的操作，诸如即时构建、生成报告，或者检索一些构建好的构件。</p>
</li>
</ol>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Jenkins就是这么一个CI系统。之前叫做Hudson。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;以下是使用Jenkins的一些理由：</p>
<ul>
<li>是所有CI产品中在安装和配置上最简单的。</li>
<li>基于Web访问，用户界面非常友好、直观和灵活，在许多情况下，还提供了AJAX的即时反馈。</li>
<li>Jenkins是基于Java开发的(如果你是一个Java开发人员，这是非常有用的)，但它不仅限于构建基于Java的软件。</li>
<li>Jenkins拥有大量的插件。这些插件极大的扩展了Jenkins的功能；它们都是开源的，而且它们可以直接通过web界面来进行安装与管理。</li>
</ul>
<h2 id="Jenkins的目标"><a href="#Jenkins的目标" class="headerlink" title="Jenkins的目标"></a>Jenkins的目标</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Jenkins的主要目标是监控软件开发流程，快速显示问题。所以能保证开发人员以及相关人员省时省力提高开发效率。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;CI系统在整个开发过程中的主要作用是控制：当系统在代码存储库中探测到修改时，它将运行构建的任务委托给构建过程本身。如果构建失败了，那么CI系统将通知相关人员，然后继续监视存储库。它的角色看起来是被动的；但它确能快速反映问题。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;特别是它具有以下优点：</p>
<ul>
<li>Jenkins一切配置都可以在web界面上完成。有些配置如MAVEN_HOME和Email，只需要配置一次，所有的项目就都能用。当然也可以通过修改XML进行配置。</li>
<li>支持Maven的模块(Module)，Jenkins对Maven做了优化，因此它能自动识别Module，每个Module可以配置成一个job。相当灵活。</li>
<li>测试报告聚合，所有模块的测试报告都被聚合在一起，结果一目了然，使用其他CI，这几乎是件不可能完成的任务。</li>
<li>构件指纹(artifact fingerprint)，每次build的结果构件都被很好的自动管理，无需任何配置就可以方便的浏览下载。</li>
</ul>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/HA集群/5. Mysql-5.5+Heartbeat-3.0.5+DRBD/">Mysql-5.5+Heartbeat-3.0.5+DRBD</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/HA集群/">HA集群</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;环境：</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;CentOS 6.5</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;MySQL_Master  </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;eth0  192.168.1.10</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;eth1  192.168.2.10        </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;MySQL_Slave    </p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;eth0  192.168.1.11</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;eth1  192.168.2.11</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;HA    192.168.1.254</p>
<h2 id="创建RAID1-0组合-存储数据-四块硬盘"><a href="#创建RAID1-0组合-存储数据-四块硬盘" class="headerlink" title="创建RAID1+0组合[存储数据 四块硬盘]"></a>创建RAID1+0组合[存储数据 四块硬盘]</h2><h3 id="1-创建两块RAID1-4块硬盘、MySQL主从节点执行"><a href="#1-创建两块RAID1-4块硬盘、MySQL主从节点执行" class="headerlink" title="1.创建两块RAID1 [4块硬盘、MySQL主从节点执行]"></a>1.创建两块RAID1 [4块硬盘、MySQL主从节点执行]</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># mdadm --create /dev/md0 --level=raid1 --raid-devices=2 /dev/sdb1 /dev/sdc1</span></div><div class="line">mdadm: Note: this array has metadata at the start and</div><div class="line"> may not be suitable as a boot device. If you plan to</div><div class="line"> store <span class="string">'/boot'</span> on this device please ensure that</div><div class="line"> your boot-loader understands md/v1.x metadata, or use</div><div class="line"> --metadata=0.90</div><div class="line">Continue creating array? y</div><div class="line">mdadm: Defaulting to version 1.2 metadata</div><div class="line">mdadm: array /dev/md0 started.</div><div class="line">[root@master ~]<span class="comment"># mdadm --create /dev/md1 --level=raid1 --raid-devices=2 /dev/sdb1 /dev/sdc1</span></div><div class="line">mdadm: Note: this array has metadata at the start and</div><div class="line"> may not be suitable as a boot device. If you plan to</div><div class="line"> store <span class="string">'/boot'</span> on this device please ensure that</div><div class="line"> your boot-loader understands md/v1.x metadata, or use</div><div class="line"> --metadata=0.90</div><div class="line">Continue creating array? y</div><div class="line">mdadm: Defaulting to version 1.2 metadata</div><div class="line">mdadm: array /dev/md1 started.</div></pre></td></tr></table></figure>
<h3 id="2-利用两个-RAID1-创建-RAID0"><a href="#2-利用两个-RAID1-创建-RAID0" class="headerlink" title="2.利用两个 RAID1 创建 RAID0"></a>2.利用两个 RAID1 创建 RAID0</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@CentOS ~]<span class="comment"># mdadm --create /dev/md2 --level=raid0 --raid-devices=2 /dev/md0 /dev/md1</span></div><div class="line">mdadm: Defaulting to version 1.2 metadata</div><div class="line">mdadm: array /dev/md2 started.</div></pre></td></tr></table></figure>
<h3 id="3-将raid信息写入配置文件"><a href="#3-将raid信息写入配置文件" class="headerlink" title="3.将raid信息写入配置文件"></a>3.将raid信息写入配置文件</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># mdadm --detail --scan &gt; /etc/mdadm.conf </span></div><div class="line">[root@master ~]<span class="comment"># vi /etc/mdadm.conf</span></div><div class="line">ARRAY /dev/md0 metadata=1.2 name=CentOS:0 UUID=b4c4c7b4:0f9f6e60:7eb24578:29682c96</div><div class="line">devices /dev/sdb1 /dev/sdc1</div><div class="line">ARRAY /dev/md1 metadata=1.2 name=CentOS:1 UUID=f5afcda6:86847677:c752fcdd:fbb91e00</div><div class="line">devices /dev/sdd1 /dev/sde1</div><div class="line">ARRAY /dev/md2 metadata=1.2 name=CentOS:2 UUID=00f120ec:bab2f3fe:80d88cb9:3ee4b76b</div><div class="line">devices /dev/md0 /dev/md1</div></pre></td></tr></table></figure>
<h2 id="安装DRBD"><a href="#安装DRBD" class="headerlink" title="安装DRBD"></a>安装DRBD</h2><h3 id="1-解压并安装DRBD"><a href="#1-解压并安装DRBD" class="headerlink" title="1.解压并安装DRBD"></a>1.解压并安装DRBD</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">[root@master Linux]<span class="comment"># yum -y install gcc kernel-devel kernel-headers flex perl</span></div><div class="line">[root@master Linux]<span class="comment"># http://oss.linbit.com/drbd/8.4/drbd-8.4.4.tar.gz</span></div><div class="line">[root@master Linux]<span class="comment"># tar fzvx drbd-8.4.4.tar.gz</span></div><div class="line">[root@master Linux]<span class="comment"># cd drbd-8.4.4</span></div><div class="line">[root@master drbd-8.4.4]<span class="comment"># ./configure --prefix=/usr/local/drbd-8.4 --with-km</span></div><div class="line">[root@master drbd-8.4.4]<span class="comment"># make KDIR=/usr/src/kernels/2.6.32-358.el6.x86_64/</span></div><div class="line">[root@master drbd-8.4.4]<span class="comment"># make install</span></div><div class="line">[root@master drbd-8.4.4<span class="comment"># mkdir -p /usr/local/drbd-8.4/var/run/drbd</span></div><div class="line">[root@master drbd-8.4.4]<span class="comment"># cp /usr/local/drbd-8.4/etc/rc.d/init.d/drbd /etc/init.d/</span></div><div class="line">[root@master drbd-8.4.4]<span class="comment"># chkconfig --add drbd</span></div><div class="line">[root@master drbd-8.4.4]<span class="comment"># chkconfig drbd on</span></div></pre></td></tr></table></figure>
<h3 id="2-安装drbd模块"><a href="#2-安装drbd模块" class="headerlink" title="2.安装drbd模块"></a>2.安装drbd模块</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@master drbd-8.4.4]<span class="comment"># cd drbd</span></div><div class="line">[root@master drbd]<span class="comment"># make clean</span></div><div class="line">[root@master drbd]<span class="comment"># make KDIR=/usr/src/kernels/2.6.32-358.el6.x86_64/</span></div><div class="line">[root@master drbd]<span class="comment"># cp drbd.ko /lib/modules/2.6.32-358.el6.x86_64/kernel/lib/</span></div><div class="line">[root@master drbd]<span class="comment"># depmod</span></div></pre></td></tr></table></figure>
<h3 id="3-配置global-common-conf"><a href="#3-配置global-common-conf" class="headerlink" title="3.配置global_common.conf"></a>3.配置global_common.conf</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div></pre></td><td class="code"><pre><div class="line">[root@master drbd]<span class="comment"># cd /usr/local/drbd-8.4/etc/drbd.d/</span></div><div class="line">[root@master drbd.d]<span class="comment"># cp global_common.conf global_common.conf.bak</span></div><div class="line">[root@master drbd.d]<span class="comment"># vi global_common.conf</span></div><div class="line">global &#123;</div><div class="line">usage-count no;</div><div class="line">&#125;</div><div class="line">common &#123;</div><div class="line">handlers &#123;</div><div class="line">pri-on-incon-degr <span class="string">"/usr/lib/drbd/notify-pri-on-incon-degr.sh; /usr/lib/drbd/notify-emergency-reboot.sh; echo b &gt; /proc/sysrq-trigger ; reboot -f"</span>;</div><div class="line">pri-lost-after-sb <span class="string">"/usr/lib/drbd/notify-pri-lost-after-sb.sh; /usr/lib/drbd/notify-emergency-reboot.sh; echo b &gt; /proc/sysrq-trigger ; reboot -f"</span>;</div><div class="line"><span class="built_in">local</span>-io-error <span class="string">"/usr/lib/drbd/notify-io-error.sh; /usr/lib/drbd/notify-emergency-shutdown.sh; echo o &gt; /proc/sysrq-trigger ; halt -f"</span>;</div><div class="line">fence-peer <span class="string">"/usr/lib/drbd/crm-fence-peer.sh"</span>;</div><div class="line">split-brain <span class="string">"/usr/lib/drbd/notify-split-brain.sh root"</span>;</div><div class="line">out-of-sync <span class="string">"/usr/lib/drbd/notify-out-of-sync.sh root"</span>;</div><div class="line">&#125;</div><div class="line">startup &#123;</div><div class="line">wfc-timeout 30;</div><div class="line">degr-wfc-timeout 30;</div><div class="line">outdated-wfc-timeout 30;</div><div class="line">&#125;</div><div class="line">disk &#123;</div><div class="line"><span class="comment">#磁盘读写速度与同步速率的30%</span></div><div class="line">resync-rate 30M;</div><div class="line">on-io-error detach;</div><div class="line">fencing resource-only;</div><div class="line">&#125;</div><div class="line">net &#123;</div><div class="line">protocol C;</div><div class="line">cram-hmac-alg sha1;</div><div class="line">shared-secret <span class="string">"mysql-ha"</span>;</div><div class="line">csums-alg sha1;</div><div class="line">verify-alg crc32c;</div><div class="line">&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="4-创建r0资源"><a href="#4-创建r0资源" class="headerlink" title="4.创建r0资源"></a>4.创建r0资源</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">[root@master drbd.d]<span class="comment"># vi r0.res</span></div><div class="line">resource r0&#123;</div><div class="line"> on master&#123;</div><div class="line"> device /dev/drbd0; <span class="comment">#逻辑设备的路径</span></div><div class="line"> disk /dev/md2; <span class="comment">#物理设备</span></div><div class="line"> address  192.168.2.10:7788;</div><div class="line"> meta-disk  internal;</div><div class="line"> &#125;</div><div class="line"> on slave&#123;</div><div class="line"> device /dev/drbd0;</div><div class="line"> disk /dev/md2;</div><div class="line"> address  192.168.2.11:7788;</div><div class="line"> meta-disk  internal;</div><div class="line"> &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="5-建立-drbd-resource"><a href="#5-建立-drbd-resource" class="headerlink" title="5.建立 drbd resource"></a>5.建立 drbd resource</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@master drbd.d]<span class="comment"># modprobe drbd</span></div><div class="line">[root@master drbd.d]<span class="comment"># drbdadm create-md r0</span></div><div class="line">[root@master drbd.d]<span class="comment"># drbdadm up r0</span></div></pre></td></tr></table></figure>
<h3 id="6-设置Primary-在master节点操作"><a href="#6-设置Primary-在master节点操作" class="headerlink" title="6.设置Primary [在master节点操作]"></a>6.设置Primary [在master节点操作]</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master drbd.d]<span class="comment"># drbdadm primary --force r0</span></div></pre></td></tr></table></figure>
<h3 id="7-创建DRBD文件系统-在Mysql主节点的master上执行"><a href="#7-创建DRBD文件系统-在Mysql主节点的master上执行" class="headerlink" title="7.创建DRBD文件系统 [在Mysql主节点的master上执行]"></a>7.创建DRBD文件系统 [在Mysql主节点的master上执行]</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@master drbd.d]<span class="comment"># mkfs.ext4 /dev/drbd0</span></div><div class="line">[root@master drbd.d]<span class="comment"># mount /dev/drbd0 /raid10/</span></div></pre></td></tr></table></figure>
<h3 id="8-DRBD同步测试"><a href="#8-DRBD同步测试" class="headerlink" title="8.DRBD同步测试"></a>8.DRBD同步测试</h3><h4 id="首先，在主服务器上先将设备卸载，同时将主服务器降为备用服务器："><a href="#首先，在主服务器上先将设备卸载，同时将主服务器降为备用服务器：" class="headerlink" title="首先，在主服务器上先将设备卸载，同时将主服务器降为备用服务器："></a>首先，在主服务器上先将设备卸载，同时将主服务器降为备用服务器：</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@master drbd]<span class="comment"># mkdir -p /raid10/mysql/data</span></div><div class="line">[root@master drbd]<span class="comment"># cd /</span></div><div class="line">[root@master /]<span class="comment"># umount /dev/drbd0</span></div><div class="line">[root@master /]<span class="comment"># drbdadm secondary r0</span></div></pre></td></tr></table></figure>
<h4 id="然后，登录备用服务器，将备用服务器升为主服务器，同时挂载drbd0设备到-raid10目录："><a href="#然后，登录备用服务器，将备用服务器升为主服务器，同时挂载drbd0设备到-raid10目录：" class="headerlink" title="然后，登录备用服务器，将备用服务器升为主服务器，同时挂载drbd0设备到 /raid10目录："></a>然后，登录备用服务器，将备用服务器升为主服务器，同时挂载drbd0设备到 /raid10目录：</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@slave drbd]<span class="comment"># drbdadm up r0</span></div><div class="line">[root@slave drbd]<span class="comment"># drbdadm primary r0</span></div><div class="line">[root@slave drbd]<span class="comment"># mount /dev/drbd0 /raid10/</span></div><div class="line">[root@slave drbd]<span class="comment"># cd /raid10/</span></div><div class="line">[root@slave raid10]<span class="comment"># ls</span></div><div class="line">lost+found mysql</div></pre></td></tr></table></figure>
<h2 id="使用中出现脑裂以及解决办法"><a href="#使用中出现脑裂以及解决办法" class="headerlink" title="使用中出现脑裂以及解决办法"></a>使用中出现脑裂以及解决办法</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@slave ~]<span class="comment"># cat /proc/drbd</span></div><div class="line">version: 8.4.4 (api:1/proto:86-101)</div><div class="line">GIT-hash: 599f286440bd633d15d5ff985204aff4bccffadd build by root@slave, 2013-12-03 09:50:30 0: cs:StandAlone ro:Primary/Unknown ds:UpToDate/Outdated r-----</div><div class="line">ns:0 nr:0 dw:2 dr:1684 al:1 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:4</div></pre></td></tr></table></figure>
<h3 id="查看日志"><a href="#查看日志" class="headerlink" title="查看日志"></a>查看日志</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">[root@slave /]<span class="comment"># tail -f /var/log/messages</span></div><div class="line">Dec  3 11:06:05 slave kernel: block drbd0: helper <span class="built_in">command</span>: /sbin/drbdadm split-brain minor-0 <span class="built_in">exit</span> code</div><div class="line">127 (0x7f00)</div><div class="line">Dec  3 11:06:05 slave kernel: drbd r0: conn( WFReportParams -&gt; Disconnecting )</div><div class="line">Dec  3 11:06:05 slave kernel: drbd r0: error receiving ReportState, e: -5 l: 0!</div><div class="line">Dec  3 11:06:05 slave kernel: drbd r0: asender terminated</div><div class="line">Dec  3 11:06:05 slave kernel: drbd r0: Terminating drbd_a_r0</div><div class="line">Dec  3 11:06:05 slave kernel: drbd r0: Connection closed</div><div class="line">Dec  3 11:06:05 slave kernel: drbd r0: conn( Disconnecting -&gt; StandAlone )</div><div class="line">Dec  3 11:06:05 slave kernel: drbd r0: receiver terminated</div><div class="line">Dec  3 11:06:05 slave kernel: drbd r0: Terminating drbd_r_r0</div><div class="line">Dec  3 11:06:41 slave kernel: block drbd0: role( Secondary -&gt; Primary )</div></pre></td></tr></table></figure>
<h2 id="解决方法："><a href="#解决方法：" class="headerlink" title="解决方法："></a>解决方法：</h2><h3 id="1-需要将现在的master角色修改为secondary"><a href="#1-需要将现在的master角色修改为secondary" class="headerlink" title="1.需要将现在的master角色修改为secondary"></a>1.需要将现在的master角色修改为secondary</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># drbdadm secondary r0</span></div><div class="line"><span class="comment">#该命令告诉slave，secondary上的数据不正确，以primary上的数据为准。</span></div><div class="line">[root@master ~]<span class="comment"># drbdadm -- --discard-my-data connect r0</span></div></pre></td></tr></table></figure>
<h3 id="2-我们还需要在slave上执行下面操作"><a href="#2-我们还需要在slave上执行下面操作" class="headerlink" title="2.我们还需要在slave上执行下面操作"></a>2.我们还需要在slave上执行下面操作</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#这样master就能和slave开始连接上了，并且保证数据不会丢失：</span></div><div class="line">[root@slave ~]<span class="comment"># drbdadm connect r0</span></div><div class="line">[root@slave ~]<span class="comment"># cat /proc/drbd</span></div><div class="line">version: 8.4.4 (api:1/proto:86-101)</div><div class="line">GIT-hash: 599f286440bd633d15d5ff985204aff4bccffadd build by root@slave, 2013-12-03 09:50:30</div><div class="line"> 0: cs:Connected ro:Secondary/Primary ds:UpToDate/UpToDate C r-----</div><div class="line"> ns:0 nr:4 dw:6 dr:1688 al:1 bm:1 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0</div><div class="line">[root@master ~]<span class="comment"># cat /proc/drbd</span></div><div class="line">version: 8.4.4 (api:1/proto:86-101)</div><div class="line">GIT-hash: 599f286440bd633d15d5ff985204aff4bccffadd build by root@master, 2013-12-03 09:49:22</div><div class="line"> 0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----</div><div class="line"> ns:4 nr:0 dw:1 dr:1015 al:1 bm:1 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0</div></pre></td></tr></table></figure>
<h2 id="安装mysql"><a href="#安装mysql" class="headerlink" title="安装mysql"></a>安装mysql</h2><h3 id="1-mastr节点"><a href="#1-mastr节点" class="headerlink" title="1.mastr节点"></a>1.mastr节点</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div></pre></td><td class="code"><pre><div class="line">[root@master Linux]<span class="comment"># yum install gcc gcc-c++ autoconf automake ncurses-devel libtool-ltdl-devel* -y</span></div><div class="line">[root@master Linux]<span class="comment"># useradd -M -s /sbin/nologin mysql</span></div><div class="line">[root@master Linux]<span class="comment"># tar zfvx cmake-2.8.12.tar.gz</span></div><div class="line">[root@master Linux]<span class="comment"># cd cmake-2.8.12</span></div><div class="line">[root@master cmake-2.8.12]<span class="comment"># ./configure</span></div><div class="line">[root@master cmake-2.8.12]<span class="comment"># gmake &amp;&amp; make install</span></div><div class="line">[root@master cmake-2.8.12]<span class="comment"># cd ..</span></div><div class="line">[root@master Linux]<span class="comment"># tar zfxv mysql-5.5.25.tar.gz</span></div><div class="line">[root@master Linux]<span class="comment"># cd mysql-5.5.25</span></div><div class="line">[root@master mysql-5.5.25]<span class="comment"># cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.5</span></div><div class="line">[root@master mysql-5.5.25]<span class="comment"># make &amp;&amp; make install</span></div><div class="line">[root@master mysql-5.5.25]<span class="comment"># cp support-files/my-medium.cnf /raid10/mysql/my.cf</span></div><div class="line">[root@master mysql-5.5.25]<span class="comment"># rm -rf /etc/my.cnf</span></div><div class="line">[root@master mysql-5.5.25]<span class="comment"># ln -sv /raid10/mysql/my.cf /etc/</span></div><div class="line">[root@master mysql-5.5.25]<span class="comment"># cd /usr/local/mysql-5.5/</span></div><div class="line">[root@master mysql-5.5]<span class="comment"># chown -R root:mysql .</span></div><div class="line">[root@master mysql-5.5]<span class="comment"># chown -R mysql:mysql /raid10/mysql/data/</span></div><div class="line">[root@master mysql-5.5]<span class="comment"># ./scripts/mysql_install_db --user=mysql \</span></div><div class="line">--basedir=/usr/<span class="built_in">local</span>/mysql-5.5/ \</div><div class="line">--datadir=/raid10/mysql/data/</div><div class="line">[root@master mysql-5.5]<span class="comment"># cp support-files/mysql.server /etc/init.d/mysqld</span></div><div class="line">[root@master mysql-5.5]<span class="comment"># chmod +x /etc/init.d/mysqld</span></div><div class="line">[root@master mysql-5.5]<span class="comment"># chkconfig --add mysqld</span></div><div class="line">[root@master mysql-5.5]<span class="comment"># vi /etc/init.d/mysqld</span></div><div class="line">datadir=/raid10/mysql/data</div></pre></td></tr></table></figure>
<h3 id="2-savle节点安装"><a href="#2-savle节点安装" class="headerlink" title="2.savle节点安装"></a>2.savle节点安装</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">——安装mysql[MySQL主节点的savle节点安装]</div><div class="line">[root@slave Linux]<span class="comment"># yum install gcc gcc-c++ autoconf automake ncurses-devel libtool-ltdl-devel* -y</span></div><div class="line">[root@slaveLinux]<span class="comment"># useradd -M -s /sbin/nologin mysql</span></div><div class="line">[root@slave Linux]<span class="comment"># tar zfvx cmake-2.8.12.tar.gz</span></div><div class="line">[root@slave Linux]<span class="comment"># cd cmake-2.8.12</span></div><div class="line">[root@slavecmake-2.8.12]<span class="comment"># ./configure</span></div><div class="line">[root@slavecmake-2.8.12]<span class="comment"># gmake &amp;&amp; make install</span></div><div class="line">[root@slavecmake-2.8.12]<span class="comment"># cd ..</span></div><div class="line">[root@slave Linux]<span class="comment"># tar zfxv mysql-5.5.25.tar.gz</span></div><div class="line">[root@slave Linux]<span class="comment"># cd mysql-5.5.25</span></div><div class="line">[root@slave mysql-5.5.25]<span class="comment"># cmake -DCMAKE_INSTALL_PREFIX=/usr/local/mysql-5.5</span></div><div class="line">[root@slave mysql-5.5.25]<span class="comment"># make &amp;&amp; make install</span></div><div class="line">[root@slave mysql-5.5.25]<span class="comment"># cd /usr/local/mysql-5.5/</span></div><div class="line">[root@slave mysql-5.5]<span class="comment"># chown -R root:mysql .</span></div><div class="line">[root@slave mysql-5.5]<span class="comment"># cp support-files/mysql.server /etc/init.d/mysqld</span></div><div class="line">[root@slave mysql-5.5]<span class="comment"># vi /etc/init.d/mysqld</span></div><div class="line">datadir=/raid10/mysql/data</div></pre></td></tr></table></figure>
<h2 id="MySQL主节点实现高可用"><a href="#MySQL主节点实现高可用" class="headerlink" title="MySQL主节点实现高可用"></a>MySQL主节点实现高可用</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意：以下操作在MySQL主节点的master、slave节点执行</p>
<h3 id="1-添加hosts主机信息"><a href="#1-添加hosts主机信息" class="headerlink" title="1.添加hosts主机信息"></a>1.添加hosts主机信息</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@master Linux]<span class="comment"># vi /etc/hosts</span></div><div class="line">192.168.2.10  master</div><div class="line">192.168.2.11  slave</div></pre></td></tr></table></figure>
<h3 id="2-添加用户和组"><a href="#2-添加用户和组" class="headerlink" title="2.添加用户和组"></a>2.添加用户和组</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@master Linux]<span class="comment"># groupadd haclient</span></div><div class="line">[root@master Linux]<span class="comment"># useradd -g haclient -M -s /sbin/nologin hacluster</span></div></pre></td></tr></table></figure>
<h3 id="3-安装heartbeat"><a href="#3-安装heartbeat" class="headerlink" title="3.安装heartbeat"></a>3.安装heartbeat</h3><h4 id="安装相关软件依赖包"><a href="#安装相关软件依赖包" class="headerlink" title="安装相关软件依赖包"></a>安装相关软件依赖包</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[root@master Linux]<span class="comment"># yum install libtool automake autoconf \</span></div><div class="line">glib2-devel \</div><div class="line">libxml2-devel \</div><div class="line">bzip2-devel \</div><div class="line">libtool-ltdl-devel \</div><div class="line">libxslt-devel \</div><div class="line">docbook* -y</div></pre></td></tr></table></figure>
<h4 id="安装glue"><a href="#安装glue" class="headerlink" title="安装glue"></a>安装glue</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@master Linux]<span class="comment"># wget http://hg.linux-ha.org/glue/archive/glue-1.0.9.tar.bz2</span></div><div class="line">[root@master Linux]<span class="comment"># tar jfvx glue-1.0.9.tar.bz2</span></div><div class="line">[root@master Linux]<span class="comment"># cd Reusable-Cluster-Components-glue--glue-1.0.9/</span></div><div class="line">[root@master Reusable-Cluster-Components-glue--glue-1.0.9]<span class="comment"># ./autogen.sh</span></div><div class="line">[root@master Reusable-Cluster-Components-glue--glue-1.0.9]<span class="comment"># ./configure LIBS='/lib64/libuuid.so.1'</span></div><div class="line">[root@CentOS Reusable-Cluster-Components-glue--glue-1.0.9]<span class="comment"># make &amp;&amp; make install</span></div></pre></td></tr></table></figure>
<h4 id="安装agents"><a href="#安装agents" class="headerlink" title="安装agents"></a>安装agents</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[root@master Heartbeat-3-0-7e3a82377fa8]<span class="comment"># cd ..</span></div><div class="line">[root@master Linux]<span class="comment"># wget https://codeload.github.com/ClusterLabs/resource-agents/legacy.tar.gz/v3.9.2</span></div><div class="line">[root@master Linux]<span class="comment"># tar zfvx ClusterLabs-resource-agents-v3.9.2-0-ge261943.tar.gz</span></div><div class="line">[root@master Linux]<span class="comment"># cd ClusterLabs-resource-agents-b735277/</span></div><div class="line">[root@master ClusterLabs-resource-agents-b735277]<span class="comment"># ./autogen.sh</span></div><div class="line">[root@master ClusterLabs-resource-agents-b735277]<span class="comment"># ./configure LIBS='/lib64/libuuid.so.1'</span></div><div class="line">[root@master ClusterLabs-resource-agents-b735277]<span class="comment"># make &amp;&amp; make install</span></div></pre></td></tr></table></figure>
<h4 id="安装heartbeat"><a href="#安装heartbeat" class="headerlink" title="安装heartbeat"></a>安装heartbeat</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[root@master ClusterLabs-resource-agents-b735277]<span class="comment"># cd ..</span></div><div class="line">[root@master Linux]<span class="comment"># wget http://hg.linux-ha.org/heartbeat-STABLE_3_0/archive/7e3a82377fa8.tar.bz2</span></div><div class="line">[root@master Linux]<span class="comment"># tar jfvx heartbeat-3.0.5.tar.bz2</span></div><div class="line">[root@master Linux]<span class="comment"># cd Heartbeat-3-0-7e3a82377fa8/</span></div><div class="line">[root@master Heartbeat-3-0-7e3a82377fa8]<span class="comment"># ./bootstrap</span></div><div class="line">[root@master Heartbeat-3-0-7e3a82377fa8]<span class="comment"># ./ConfigureMe configure LIBS='/lib64/libuuid.so.1'</span></div><div class="line">[root@master Heartbeat-3-0-7e3a82377fa8]<span class="comment"># make &amp;&amp; make install</span></div></pre></td></tr></table></figure>
<h3 id="4-配置heartbeat"><a href="#4-配置heartbeat" class="headerlink" title="4.配置heartbeat"></a>4.配置heartbeat</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div></pre></td><td class="code"><pre><div class="line">[root@master Heartbeat-3-0-7e3a82377fa8]<span class="comment"># cd doc</span></div><div class="line">[root@master doc]<span class="comment"># cp authkeys haresources ha.cf /etc/ha.d/</span></div><div class="line">[root@master doc]<span class="comment"># cp /usr/etc/ha.d/shellfuncs /etc/ha.d/</span></div><div class="line">[root@master doc]<span class="comment"># rm -rf /usr/etc/ha.d</span></div><div class="line">[root@master doc]<span class="comment"># ln -sv /etc/ha.d /usr/etc</span></div><div class="line">[root@master doc]<span class="comment"># vi /etc/ha.d/ha.cf</span></div><div class="line"><span class="comment">#开启日志</span></div><div class="line">logfile /var/<span class="built_in">log</span>/ha-log</div><div class="line"><span class="comment">#设置syslog()/logger设备</span></div><div class="line">logfacility local0</div><div class="line"><span class="comment">#心跳发送时间间隔/秒</span></div><div class="line">keepalive 2</div><div class="line"><span class="comment">#15秒没有收到主机心跳、确认主机故障</span></div><div class="line">deadtime 15</div><div class="line"><span class="comment">#警告次数</span></div><div class="line">warntime 5</div><div class="line"><span class="comment">#守护进程启动30后 启动服务资源</span></div><div class="line">initdead 30</div><div class="line"><span class="comment">#监听端口</span></div><div class="line">udpport 694</div><div class="line"><span class="comment">#另一个节点IP、通过检测来保证心跳的可用性</span></div><div class="line">ucast eth0 192.168.1.11</div><div class="line">ucast eth1 192.168.2.11</div><div class="line"><span class="comment">#两个节点的名字 [uname -n 获取]</span></div><div class="line">node master</div><div class="line">node slave</div><div class="line"><span class="comment">#开启DPOD</span></div><div class="line">respawn hacluster /usr/lib64/heartbeat/ipfail</div><div class="line">respawn hacluster /usr/lib64/heartbeat/dopd</div><div class="line">apiauth ipfail gid=haclient uid=hacluster</div><div class="line">apiauth dopd gid=haclient uid=hacluster</div><div class="line">[root@master doc]<span class="comment"># vi /etc/ha.d/authkeys</span></div><div class="line">auth 1</div><div class="line">1 sha1 HI!</div><div class="line">[root@master doc]<span class="comment"># chmod 600 /etc/ha.d/authkeys</span></div><div class="line">[root@master doc]<span class="comment"># vi /etc/ha.d/haresources</span></div><div class="line">master drbddisk::r0 Filesystem::/dev/drbd0::/raid10::ext4 mysqld IPaddr::192.168.1.254/24/eth0</div><div class="line">[root@master doc]<span class="comment"># cd /Linux/drbd-8.4.4/scripts/</span></div><div class="line">[root@master scripts]<span class="comment"># cp drbddisk /etc/ha.d/resource.d/</span></div><div class="line">[root@master scripts]<span class="comment"># cp /etc/init.d/mysqld /etc/ha.d/resource.d/</span></div><div class="line">[root@master scripts]<span class="comment"># chkconfig --add heartbeat</span></div><div class="line">[root@master scripts]<span class="comment"># chkconfig heartbeat on</span></div></pre></td></tr></table></figure>
<h2 id="测试"><a href="#测试" class="headerlink" title="测试"></a>测试</h2><h3 id="1-启动master节点启动heartbeat"><a href="#1-启动master节点启动heartbeat" class="headerlink" title="1.启动master节点启动heartbeat"></a>1.启动master节点启动heartbeat</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master scripts]<span class="comment"># /etc/init.d/heartbeat start</span></div></pre></td></tr></table></figure>
<h3 id="2-启动slave节点启动heartbeat"><a href="#2-启动slave节点启动heartbeat" class="headerlink" title="2.启动slave节点启动heartbeat"></a>2.启动slave节点启动heartbeat</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@slave scripts]<span class="comment"># /etc/init.d/heartbeat start</span></div></pre></td></tr></table></figure>
<h2 id="3-在master节点上查看启动日志"><a href="#3-在master节点上查看启动日志" class="headerlink" title="3.在master节点上查看启动日志"></a>3.在master节点上查看启动日志</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">[root@master ha.d]<span class="comment"># tail -f /var/log/ha-log</span></div><div class="line">Apr 26 21:11:48 master heartbeat: [42033]: info: Starting child client <span class="string">"/usr/lib64/heartbeat/ipfail"</span> (501,501)</div><div class="line">Apr 26 21:11:48 master heartbeat: [42033]: info: Starting child client <span class="string">"/usr/lib64/heartbeat/dopd"</span> (501,501)</div><div class="line">Apr 26 21:11:48 mster heartbeat: [42062]: info: Starting <span class="string">"/usr/lib64/heartbeat/ipfail"</span> as uid 501  gid 501 (pid 42062)</div><div class="line">Apr 26 21:11:48 mster heartbeat: [42064]: info: Starting <span class="string">"/usr/lib64/heartbeat/dopd"</span> as uid 501  gid 501 (pid 42064)</div><div class="line">Apr 26 21:11:48 mster heartbeat: [42061]: info: Local Resource acquisition completed.</div><div class="line">Apr 26 21:11:48 mster heartbeat: [42033]: info: Initial resource acquisition complete (req_our_resources)</div><div class="line">Apr 26 21:11:48 mster ipfail: [42062]: ERROR: auto_failback <span class="built_in">set</span> to incompatible legacy option.</div><div class="line">Apr 26 21:11:48 mster heartbeat: [42033]: WARN: Managed /usr/lib64/heartbeat/ipfail process 42062 exited with <span class="built_in">return</span> code 100.</div><div class="line">Apr 26 21:11:48 mster heartbeat: [42033]: info: Status update <span class="keyword">for</span> slave: status active</div><div class="line">harc[42104]:  2014/04/26_21:11:48 info: Running /usr/etc/ha.d//rc.d/status status</div><div class="line"><span class="comment">#说明启动成功</span></div></pre></td></tr></table></figure>
<h3 id="4-切换主备"><a href="#4-切换主备" class="headerlink" title="4.切换主备"></a>4.切换主备</h3><h4 id="在master节点停掉heartbeat"><a href="#在master节点停掉heartbeat" class="headerlink" title="在master节点停掉heartbeat"></a>在master节点停掉heartbeat</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master scripts]<span class="comment"># /etc/init.d/heartbeat stop</span></div></pre></td></tr></table></figure>
<h4 id="查看slave日志是否能自动切换至slave节点"><a href="#查看slave日志是否能自动切换至slave节点" class="headerlink" title="查看slave日志是否能自动切换至slave节点"></a>查看slave日志是否能自动切换至slave节点</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div></pre></td><td class="code"><pre><div class="line">[root@slave scripts]<span class="comment"># tail -f /var/log/ha-log</span></div><div class="line"><span class="comment">#显示master节点已经shutdown</span></div><div class="line">Apr 26 21:21:27 slave heartbeat: [42054]: info: Received shutdown notice from <span class="string">'master'</span>.</div><div class="line">Apr 26 21:21:27 slave heartbeat: [42054]: info: Resources being acquired from master.</div><div class="line">harc[42118]:  2014/04/26_21:21:27 info: Running /usr/etc/ha.d//rc.d/status status</div><div class="line">Apr 26 21:21:27 slave heartbeat: [42119]: info: No <span class="built_in">local</span> resources [/usr/share/heartbeat/ResourceManager listkeys slave] to acquire.</div><div class="line">mach_down[42148]:  2014/04/26_21:21:27 info: Taking over resource group drbddisk::r0</div><div class="line">ResourceManager[42175]: 2014/04/26_21:21:27 info: Acquiring resource group: master drbddisk::r0 Filesystem::/dev/drbd0::/raid10::ext4 mysqld IPaddr::192.168.1.254/24/eth0</div><div class="line">ResourceManager[42175]: 2014/04/26_21:21:27 info: Running /etc/ha.d/resource.d/drbddisk r0 start</div><div class="line">Filesystem[42239]:  2014/04/26_21:21:27 INFO: Resource is stopped</div><div class="line">ResourceManager[42175]: 2014/04/26_21:21:27 info: Running /etc/ha.d/resource.d/Filesystem /dev/drbd0 /raid10 ext4 start</div><div class="line">Filesystem[42320]:  2014/04/26_21:21:27 INFO: Running start <span class="keyword">for</span> /dev/drbd0 on /raid10</div><div class="line">Filesystem[42312]:  2014/04/26_21:21:28 INFO: Success</div><div class="line">ResourceManager[42175]: 2014/04/26_21:21:28 info: Running /etc/ha.d/resource.d/mysqld start</div><div class="line">Apr 26 21:21:39 slave heartbeat: [42054]: WARN: node master: is dead</div><div class="line">Apr 26 21:21:39 slave heartbeat: [42054]: info: Dead node master gave up resources.</div><div class="line">Apr 26 21:21:39 slave heartbeat: [42054]: info: Resources being acquired from master.</div><div class="line">Apr 26 21:21:39 slave heartbeat: [42054]: info: Link master:eth0 dead.</div><div class="line">Apr 26 21:21:39 slave heartbeat: [42054]: info: Link master:eth1 dead.</div><div class="line">Apr 26 21:21:41 slave heartbeat: [42614]: info: No <span class="built_in">local</span> resources [/usr/share/heartbeat/ResourceManager listkeys slave] to acquire.</div><div class="line">Apr 26 21:21:41 slave heartbeat: [42054]: info: Initial resource acquisition complete (req_our_resources)</div><div class="line">IPaddr[42642]:  2014/04/26_21:21:42 INFO: Resource is stopped</div><div class="line"><span class="comment">#显示slave节点的VIP已经运行</span></div><div class="line">ResourceManager[42175]: 2014/04/26_21:21:42 info: Running /etc/ha.d/resource.d/IPaddr 192.168.1.254/24/eth0 start</div><div class="line">IPaddr[42727]:  2014/04/26_21:21:42 INFO: Using calculated netmask <span class="keyword">for</span> 192.168.1.254: 255.255.255.0</div><div class="line">IPaddr[42727]:  2014/04/26_21:21:42 INFO: <span class="built_in">eval</span> ifconfig eth0:0 192.168.1.254 netmask 255.255.255.0 broadcast 192.168.1.255</div><div class="line">IPaddr[42701]:  2014/04/26_21:21:42 INFO: Success</div><div class="line">mach_down[42148]:  2014/04/26_21:21:42 info: mach_down takeover complete <span class="keyword">for</span> node master.</div><div class="line">harc[42820]:  2014/04/26_21:21:42 info: Running /usr/etc/ha.d//rc.d/status status</div><div class="line">mach_down[42837]:  2014/04/26_21:21:42 info: Taking over resource group drbddisk::r0</div><div class="line">ResourceManager[42864]: 2014/04/26_21:21:43 info: Acquiring resource group: master drbddisk::r0 Filesystem::/dev/drbd0::/raid10::ext4 mysqld IPaddr::192.168.1.254/24/eth0</div><div class="line"><span class="comment">#显示slave的mysql和drbd服务已经启动成功</span></div><div class="line">Filesystem[42906]:  2014/04/26_21:21:43 INFO: Running OK</div><div class="line">IPaddr[42987]:  2014/04/26_21:21:43 INFO: Running OK</div><div class="line">mach_down[42837]:  2014/04/26_21:21:43 info: mach_down takeover complete <span class="keyword">for</span> node master.</div></pre></td></tr></table></figure>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/HA集群/4. DRBD安装配置、工作原理及故障恢复/">DRBD安装配置、工作原理及故障恢复</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/HA集群/">HA集群</a>
			</span>
		
	</div>

	

	
		<h2 id="一、DRBD简介"><a href="#一、DRBD简介" class="headerlink" title="一、DRBD简介"></a>一、DRBD简介</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;DRBD的全称为：Distributed ReplicatedBlock Device(DRBD)分布式块设备复制,DRBD是由内核模块和相关脚本而构成，用以构建高可用性的集群。其实现方式是通过网络来镜像整个设备。你可以把它看作是一种网络RAID。它允许用户在远程机器上建立一个本地块设备的实时镜像。</p>
<h2 id="二、DRBD是如何工作的呢"><a href="#二、DRBD是如何工作的呢" class="headerlink" title="二、DRBD是如何工作的呢?"></a>二、DRBD是如何工作的呢?</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;(DRBD Primary)负责接收数据，把数据写到本地磁盘并发送给另一台主机(DRBD Secondary)。另一个主机再将数据存到自己的磁盘中。目前，DRBD每次只允许对一个节点进行读写访问，但这对于通常的故障切换高可用集群来说已经足够用了。有可能以后的版本支持两个节点进行读写存取。</p>
<h2 id="三、DRBD与HA的关系"><a href="#三、DRBD与HA的关系" class="headerlink" title="三、DRBD与HA的关系"></a>三、DRBD与HA的关系</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一个DRBD系统由两个节点构成，与HA集群类似，也有主节点和备用节点之分，在带有主要设备的节点上，应用程序和操作系统可以运行和访问DRBD设备（/dev/drbd*）。在主节点写入的数据通过DRBD设备存储到主节点的磁盘设备中，同时，这个数据也会自动发送到备用节点对应的DRBD设备，最终写入备用节点的磁盘设备上，在备用节点上，DRBD只是将数据从DRBD设备写入到备用节点的磁盘中。现在大部分的高可用性集群都会使用共享存储，而DRBD也可以作为一个共享存储设备，使用DRBD不需要太多的硬件的投资。因为它在TCP/IP网络中运行，所以，利用DRBD作为共享存储设备，要节约很多成本，因为价格要比专用的存储网络便宜很多；其性能与稳定性方面也不错</p>
<h2 id="四、DRBD复制模式"><a href="#四、DRBD复制模式" class="headerlink" title="四、DRBD复制模式"></a>四、DRBD复制模式</h2><h3 id="协议A："><a href="#协议A：" class="headerlink" title="协议A："></a>协议A：</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;异步复制协议。一旦本地磁盘写入已经完成，数据包已在发送队列中，则写被认为是完成的。在一个节点发生故障时，可能发生数据丢失，因为被写入到远程节点上的数据可能仍在发送队列。尽管，在故障转移节点上的数据是一致的，但没有及时更新。这通常是用于地理上分开的节点</p>
<h3 id="协议B："><a href="#协议B：" class="headerlink" title="协议B："></a>协议B：</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;内存同步（半同步）复制协议。一旦本地磁盘写入已完成且复制数据包达到了对等节点则认为写在主节点上被认为是完成的。数据丢失可能发生在参加的两个节点同时故障的情况下，因为在传输中的数据可能不会被提交到磁盘</p>
<h3 id="协议C："><a href="#协议C：" class="headerlink" title="协议C："></a>协议C：</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;同步复制协议。只有在本地和远程节点的磁盘已经确认了写操作完成，写才被认为完成。没有任何数据丢失，所以这是一个群集节点的流行模式，但I / O吞吐量依赖于网络带宽</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一般使用协议C，但选择C协议将影响流量，从而影响网络时延。为了数据可靠性，我们在生产环境使用时须慎重选项使用哪一种协议</p>
<h2 id="160-160-160-160-160-160-160-160-四、-DRBD工作原理图"><a href="#160-160-160-160-160-160-160-160-四、-DRBD工作原理图" class="headerlink" title="&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;四、 DRBD工作原理图"></a>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;四、 DRBD工作原理图</h2><p>DRBD是linux的内核的存储层中的一个分布式存储系统，可用使用DRBD在两台Linux服务器之间共享块设备，共享文件系统和数据。类似于一个网络RAID-1的功能，如图所示：</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/DRBD%E5%AE%89%E8%A3%85%E9%85%8D%E7%BD%AE%E3%80%81%E5%B7%A5%E4%BD%9C%E5%8E%9F%E7%90%86%E5%8F%8A%E6%95%85%E9%9A%9C%E6%81%A2%E5%A4%8D/01.gif?raw=true" alt=""></figure></p>
<h2 id="五、环境介绍及安装前准备"><a href="#五、环境介绍及安装前准备" class="headerlink" title="五、环境介绍及安装前准备"></a>五、环境介绍及安装前准备</h2><h3 id="环境介绍："><a href="#环境介绍：" class="headerlink" title="环境介绍："></a>环境介绍：</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;系统版本：CentOS 6.4_x86_64</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;DRBD软件：drbd-8.4.3-33.el6.x86_64 drbd-kmdl-2.6.32-358.el6-8.4.3-33.el6.x86_64 <a href="http://rpmfind.net" target="_blank" rel="external">下载地址</a></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意：这里两个软件的版本必须使用一致，而drbd-kmdl的版本要与当前系统的版本相对应，当然在实际应用中需要根据自己的系统平台下载符合需要的软件版本;查看系统版本 “uname -r”</p>
<h3 id="安装前准备："><a href="#安装前准备：" class="headerlink" title="安装前准备："></a>安装前准备：</h3><h4 id="1-每个节点的主机名称须跟”uname-n”命令的执行结果一样"><a href="#1-每个节点的主机名称须跟”uname-n”命令的执行结果一样" class="headerlink" title="1.每个节点的主机名称须跟”uname -n”命令的执行结果一样"></a>1.每个节点的主机名称须跟”uname -n”命令的执行结果一样</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="comment">######NOD1节点执行</span></div><div class="line">sed -i <span class="string">'s@\(HOSTNAME=\).*@\1nod1.allen.com@g'</span> /etc/sysconfig/network</div><div class="line">hostname nod1.allen.com</div><div class="line"><span class="comment">######NOD2节点执行</span></div><div class="line">sed -i <span class="string">'s@\(HOSTNAME=\).*@\1nod2.allen.com@g'</span> /etc/sysconfig/network</div><div class="line">hostname nod2.allen.com</div><div class="line">注释：修改文件须重启系统生效，这里先修改文件然后执行命令修改主机名称可以不用重启</div></pre></td></tr></table></figure>
<h4 id="2-两个节点的主机名称和对应的IP地址可以正常解析"><a href="#2-两个节点的主机名称和对应的IP地址可以正常解析" class="headerlink" title="2.两个节点的主机名称和对应的IP地址可以正常解析"></a>2.两个节点的主机名称和对应的IP地址可以正常解析</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line"><span class="comment">######在NOD1与NOD2节点执行</span></div><div class="line">cat &gt; /etc/hosts &lt;&lt; EOF</div><div class="line">192.168.137.225 nod1.allen.com nod1</div><div class="line">192.168.137.222 nod2.allen.com nod2</div><div class="line">EOF</div></pre></td></tr></table></figure>
<h4 id="3-配置epel的yum源-下载并安装"><a href="#3-配置epel的yum源-下载并安装" class="headerlink" title="3.配置epel的yum源 下载并安装"></a>3.配置epel的yum源 <a href="http://mirrors.yun-idc.com/epel/6/x86_64/epel-release-6-8.noarch.rpm" target="_blank" rel="external">下载并安装</a></h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">######在NOD1与NOD2节点安装</span></div><div class="line">rpm -ivh epel-release-6-8.noarch.rpm</div></pre></td></tr></table></figure>
<h4 id="4-需要为两个节点分别提供大小相同的分区"><a href="#4-需要为两个节点分别提供大小相同的分区" class="headerlink" title="4.需要为两个节点分别提供大小相同的分区"></a>4.需要为两个节点分别提供大小相同的分区</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div></pre></td><td class="code"><pre><div class="line"><span class="comment">######在NOD1节点上创建分区，分区大小必须与NOD2节点保持一样</span></div><div class="line">[root@nod1 ~]<span class="comment"># fdisk /dev/sda</span></div><div class="line">Command (m <span class="keyword">for</span> <span class="built_in">help</span>): n</div><div class="line">Command action</div><div class="line"> e extended</div><div class="line"> p primary partition (1-4)</div><div class="line">p</div><div class="line">Partition number (1-4): 3</div><div class="line">First cylinder (7859-15665, default 7859):</div><div class="line">Using default value 7859</div><div class="line">Last cylinder, +cylinders or +size&#123;K,M,G&#125; (7859-15665, default 15665): +2G</div><div class="line">Command (m <span class="keyword">for</span> <span class="built_in">help</span>): w</div><div class="line">[root@nod1 ~]<span class="comment"># partx /dev/sda #让内核重新读取分区</span></div><div class="line"><span class="comment">######查看内核有没有识别分区，如果没有需要重新启动，这里没有识别需要重启系统</span></div><div class="line">[root@nod1 ~]<span class="comment"># cat /proc/partitions</span></div><div class="line">major minor  <span class="comment">#blocks name</span></div><div class="line"> 8 0 125829120 sda</div><div class="line"> 8 1 204800 sda1</div><div class="line"> 8 2 62914560 sda2</div><div class="line"> 253 0 20971520 dm-0</div><div class="line"> 253 1 2097152 dm-1</div><div class="line"> 253 2 10485760 dm-2</div><div class="line"> 253 3 20971520 dm-3</div><div class="line">[root@nod1 ~]<span class="comment"># reboot</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div></pre></td><td class="code"><pre><div class="line"><span class="comment">######在NOD2节点上创建分区，分区大小必须与NOD1节点保持一样</span></div><div class="line">[root@nod2 ~]<span class="comment"># fdisk /dev/sda</span></div><div class="line">Command (m <span class="keyword">for</span> <span class="built_in">help</span>): n</div><div class="line">Command action</div><div class="line"> e extended</div><div class="line"> p primary partition (1-4)</div><div class="line">p</div><div class="line">Partition number (1-4): 3</div><div class="line">First cylinder (7859-15665, default 7859):</div><div class="line">Using default value 7859</div><div class="line">Last cylinder, +cylinders or +size&#123;K,M,G&#125; (7859-15665, default 15665): +2G</div><div class="line">Command (m <span class="keyword">for</span> <span class="built_in">help</span>): w</div><div class="line">[root@nod2 ~]<span class="comment"># partx /dev/sda #让内核重新读取分区</span></div><div class="line"><span class="comment">######查看内核有没有识别分区，如果没有需要重新启动，这里没有识别需要重启系统</span></div><div class="line">[root@nod2 ~]<span class="comment"># cat /proc/partitions</span></div><div class="line">major minor  <span class="comment">#blocks name</span></div><div class="line"> 8 0 125829120 sda</div><div class="line"> 8 1 204800 sda1</div><div class="line"> 8 2 62914560 sda2</div><div class="line"> 253 0 20971520 dm-0</div><div class="line"> 253 1 2097152 dm-1</div><div class="line"> 253 2 10485760 dm-2</div><div class="line"> 253 3 20971520 dm-3</div><div class="line">[root@nod2 ~]<span class="comment"># reboot</span></div></pre></td></tr></table></figure>
<h2 id="六、安装并配置DRBD"><a href="#六、安装并配置DRBD" class="headerlink" title="六、安装并配置DRBD"></a>六、安装并配置DRBD</h2><h3 id="1-在NOD1与NOD2节点上安装DRBD软件包"><a href="#1-在NOD1与NOD2节点上安装DRBD软件包" class="headerlink" title="1.在NOD1与NOD2节点上安装DRBD软件包"></a>1.在NOD1与NOD2节点上安装DRBD软件包</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="comment">######NOD1</span></div><div class="line">[root@nod1 ~]<span class="comment"># ls drbd-*</span></div><div class="line">drbd-8.4.3-33.el6.x86_64.rpm drbd-kmdl-2.6.32-358.el6-8.4.3-33.el6.x86_64.rpm</div><div class="line">[root@nod1 ~]<span class="comment"># yum -y install drbd-*.rpm</span></div><div class="line"><span class="comment">######NOD2</span></div><div class="line">[root@nod2 ~]<span class="comment"># ls drbd-*</span></div><div class="line">drbd-8.4.3-33.el6.x86_64.rpm drbd-kmdl-2.6.32-358.el6-8.4.3-33.el6.x86_64.rpm</div><div class="line">[root@nod2 ~]<span class="comment"># yum -y install drbd-*.rpm</span></div></pre></td></tr></table></figure>
<h3 id="2-查看DRBD配置文件"><a href="#2-查看DRBD配置文件" class="headerlink" title="2.查看DRBD配置文件"></a>2.查看DRBD配置文件</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">ll /etc/drbd.conf;ll /etc/drbd.d/</div><div class="line">-rw-r--r-- 1 root root 133 May 14 21:12 /etc/drbd.conf <span class="comment">#主配置文件</span></div><div class="line">total 4</div><div class="line">-rw-r--r-- 1 root root 1836 May 14 21:12 global_common.conf <span class="comment">#全局配置文件</span></div><div class="line"><span class="comment">######查看主配置文件内容</span></div><div class="line">cat /etc/drbd.conf</div><div class="line"><span class="comment">######主配置文件中包含了全局配置文件及"drbd.d/"目录下以.res结尾的文件</span></div><div class="line"><span class="comment"># You can find an example in /usr/share/doc/drbd.../drbd.conf.example</span></div><div class="line">include <span class="string">"drbd.d/global_common.conf"</span>;</div><div class="line">include <span class="string">"drbd.d/*.res"</span>;</div></pre></td></tr></table></figure>
<h3 id="3-修改配置文件如下："><a href="#3-修改配置文件如下：" class="headerlink" title="3.修改配置文件如下："></a>3.修改配置文件如下：</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div></pre></td><td class="code"><pre><div class="line">[root@nod1 ~]<span class="comment">#vim /etc/drbd.d/global_common.conf</span></div><div class="line">global &#123;</div><div class="line">usage-count no; <span class="comment">#是否参加DRBD使用统计，默认为yes</span></div><div class="line"><span class="comment"># minor-count dialog-refresh disable-ip-verification</span></div><div class="line">&#125;</div><div class="line">common &#123;</div><div class="line">protocol C; <span class="comment">#使用DRBD的同步协议</span></div><div class="line">handlers &#123;</div><div class="line"><span class="comment"># These are EXAMPLE handlers only.</span></div><div class="line"><span class="comment"># They may have severe implications,</span></div><div class="line"><span class="comment"># like hard resetting the node under certain circumstances.</span></div><div class="line"><span class="comment"># Be careful when chosing your poison.</span></div><div class="line">pri-on-incon-degr <span class="string">"/usr/lib/drbd/notify-pri-on-incon-degr.sh; /usr/lib/drbd/notify-emergency-reboot.sh; echo b &gt; /proc/sysrq-trigger ; reboot -f"</span>;</div><div class="line">pri-lost-after-sb <span class="string">"/usr/lib/drbd/notify-pri-lost-after-sb.sh; /usr/lib/drbd/notify-emergency-reboot.sh; echo b &gt; /proc/sysrq-trigger ; reboot -f"</span>;</div><div class="line"><span class="built_in">local</span>-io-error <span class="string">"/usr/lib/drbd/notify-io-error.sh; /usr/lib/drbd/notify-emergency-shutdown.sh; echo o &gt; /proc/sysrq-trigger ; halt -f"</span>;</div><div class="line"><span class="comment"># fence-peer "/usr/lib/drbd/crm-fence-peer.sh";</span></div><div class="line"><span class="comment"># split-brain "/usr/lib/drbd/notify-split-brain.sh root";</span></div><div class="line"><span class="comment"># out-of-sync "/usr/lib/drbd/notify-out-of-sync.sh root";</span></div><div class="line"><span class="comment"># before-resync-target "/usr/lib/drbd/snapshot-resync-target-lvm.sh -p 15 -- -c 16k";</span></div><div class="line"><span class="comment"># after-resync-target /usr/lib/drbd/unsnapshot-resync-target-lvm.sh;</span></div><div class="line">&#125;</div><div class="line">startup &#123;</div><div class="line"><span class="comment"># wfc-timeout degr-wfc-timeout outdated-wfc-timeout wait-after-sb</span></div><div class="line">&#125;</div><div class="line">options &#123;</div><div class="line"><span class="comment"># cpu-mask on-no-data-accessible</span></div><div class="line">&#125;</div><div class="line">disk &#123;</div><div class="line">on-io-error detach; <span class="comment">#配置I/O错误处理策略为分离</span></div><div class="line"><span class="comment"># size max-bio-bvecs on-io-error fencing disk-barrier disk-flushes</span></div><div class="line"><span class="comment"># disk-drain md-flushes resync-rate resync-after al-extents</span></div><div class="line"><span class="comment"># c-plan-ahead c-delay-target c-fill-target c-max-rate</span></div><div class="line"><span class="comment"># c-min-rate disk-timeout</span></div><div class="line">&#125;</div><div class="line">net &#123;</div><div class="line">cram-hmac-alg <span class="string">"sha1"</span>; <span class="comment">#设置加密算法</span></div><div class="line">shared-secret <span class="string">"allendrbd"</span>; <span class="comment">#设置加密密钥</span></div><div class="line"><span class="comment"># protocol timeout max-epoch-size max-buffers unplug-watermark</span></div><div class="line"><span class="comment"># connect-int ping-int sndbuf-size rcvbuf-size ko-count</span></div><div class="line"><span class="comment"># allow-two-primaries cram-hmac-alg shared-secret after-sb-0pri</span></div><div class="line"><span class="comment"># after-sb-1pri after-sb-2pri always-asbp rr-conflict</span></div><div class="line"><span class="comment"># ping-timeout data-integrity-alg tcp-cork on-congestion</span></div><div class="line"><span class="comment"># congestion-fill congestion-extents csums-alg verify-alg</span></div><div class="line"><span class="comment"># use-rle</span></div><div class="line">&#125;</div><div class="line">syncer &#123;</div><div class="line">rate 1024M; <span class="comment">#设置主备节点同步时的网络速率</span></div><div class="line">&#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注释： on-io-error <strategy>策略可能为以下选项之一</strategy></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;detach 分离：这是默认和推荐的选项，如果在节点上发生底层的硬盘I/O错误，它会将设备运行在Diskless无盘模式下</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;pass_on：DRBD会将I/O错误报告到上层，在主节点上，它会将其报告给挂载的文件系统，但是在此节点上就往往忽略（因此此节点上没有可以报告的上层）</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-local-in-error：调用本地磁盘I/O处理程序定义的命令；这需要有相应的local-io-error调用的资源处理程序处理错误的命令；这就给管理员有足够自由的权力命令命令或是脚本调用local-io-error处理I/O错误</p>
<h3 id="4-添加资源文件"><a href="#4-添加资源文件" class="headerlink" title="4.添加资源文件:"></a>4.添加资源文件:</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">[root@nod1 ~]<span class="comment"># vim /etc/drbd.d/drbd.res</span></div><div class="line">resource drbd &#123;</div><div class="line"> on nod1.allen.com &#123;  <span class="comment">#第个主机说明以on开头，后面是主机名称</span></div><div class="line"> device  /dev/drbd0;<span class="comment">#DRBD设备名称</span></div><div class="line"> disk  /dev/sda3; <span class="comment">#drbd0使用的磁盘分区为"sda3"</span></div><div class="line"> address 192.168.137.225:7789; <span class="comment">#设置DRBD监听地址与端口</span></div><div class="line"> meta-disk internal;</div><div class="line"> &#125;</div><div class="line"> on nod2.allen.com &#123;</div><div class="line"> device  /dev/drbd0;</div><div class="line"> disk  /dev/sda3;</div><div class="line"> address 192.168.137.222:7789;</div><div class="line"> meta-disk internal;</div><div class="line"> &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="5-将配置文件为NOD2提供一份"><a href="#5-将配置文件为NOD2提供一份" class="headerlink" title="5.将配置文件为NOD2提供一份"></a>5.将配置文件为NOD2提供一份</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">[root@nod1 ~]<span class="comment"># scp /etc/drbd.d/&#123;global_common.conf,drbd.res&#125; nod2:/etc/drbd.d/</span></div><div class="line">The authenticity of host <span class="string">'nod2 (192.168.137.222)'</span> can<span class="string">'t be established.</span></div><div class="line"><span class="string">RSA key fingerprint is 29:d3:28:85:20:a1:1f:2a:11:e5:88:cd:25:d0:95:c7.</span></div><div class="line"><span class="string">Are you sure you want to continue connecting (yes/no)? yes</span></div><div class="line"><span class="string">Warning: Permanently added '</span>nod2<span class="string">' (RSA) to the list of known hosts.</span></div><div class="line"><span class="string">root@nod2'</span>s password:</div><div class="line">global_common.conf 100% 1943 1.9KB/s  00:00 </div><div class="line">drbd.res 100% 318 0.3KB/s  00:00</div></pre></td></tr></table></figure>
<h3 id="6-初始化资源并启动服务"><a href="#6-初始化资源并启动服务" class="headerlink" title="6.初始化资源并启动服务"></a>6.初始化资源并启动服务</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line"><span class="comment">######在NOD1节点上初始化资源并启动服务</span></div><div class="line">[root@nod1 ~]<span class="comment"># drbdadm create-md drbd</span></div><div class="line">Writing meta data...</div><div class="line">initializing activity <span class="built_in">log</span></div><div class="line">NOT initializing bitmap</div><div class="line">lk_bdev_save(/var/lib/drbd/drbd-minor-0.lkbd) failed: No such file or directory</div><div class="line">New drbd meta data block successfully created.  <span class="comment">#提示已经创建成功</span></div><div class="line">lk_bdev_save(/var/lib/drbd/drbd-minor-0.lkbd) failed: No such file or directory</div><div class="line"><span class="comment">######启动服务</span></div><div class="line">[root@nod1 ~]<span class="comment"># service drbd start</span></div><div class="line">Starting DRBD resources: [</div><div class="line"> create res: drbd</div><div class="line"> prepare disk: drbd</div><div class="line"> adjust disk: drbd</div><div class="line"> adjust net: drbd</div><div class="line">]</div><div class="line">..........</div><div class="line">***************************************************************</div><div class="line"> DRBD<span class="string">'s startup script waits for the peer node(s) to appear.</span></div><div class="line"><span class="string"> - In case this node was already a degraded cluster before the</span></div><div class="line"><span class="string"> reboot the timeout is 0 seconds. [degr-wfc-timeout]</span></div><div class="line"><span class="string"> - If the peer was available before the reboot the timeout will</span></div><div class="line"><span class="string"> expire after 0 seconds. [wfc-timeout]</span></div><div class="line"><span class="string"> (These values are for resource '</span>drbd<span class="string">'; 0 sec -&gt; wait forever)</span></div><div class="line"><span class="string"> To abort waiting enter '</span>yes<span class="string">' [ 12]: yes</span></div><div class="line"><span class="string">######查看监听端口</span></div><div class="line"><span class="string">[root@nod1 ~]# ss -tanl |grep 7789</span></div><div class="line"><span class="string">LISTEN 0 5 192.168.137.225:7789 *:*</span></div></pre></td></tr></table></figure>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line"><span class="comment">######在NOD2节点上初始化资源并启动服务</span></div><div class="line">[root@nod2 ~]<span class="comment"># drbdadm create-md drbd</span></div><div class="line">Writing meta data...</div><div class="line">initializing activity <span class="built_in">log</span></div><div class="line">NOT initializing bitmap</div><div class="line">lk_bdev_save(/var/lib/drbd/drbd-minor-0.lkbd) failed: No such file or directory</div><div class="line">New drbd meta data block successfully created.</div><div class="line">lk_bdev_save(/var/lib/drbd/drbd-minor-0.lkbd) failed: No such file or directory</div><div class="line"><span class="comment">######启动服务</span></div><div class="line">[root@nod2 ~]<span class="comment"># service drbd start</span></div><div class="line">Starting DRBD resources: [</div><div class="line"> create res: drbd</div><div class="line"> prepare disk: drbd</div><div class="line"> adjust disk: drbd</div><div class="line"> adjust net: drbd</div><div class="line">]</div><div class="line"><span class="comment">######查看监听地址与端口</span></div><div class="line">[root@nod2 ~]<span class="comment"># netstat -anput|grep 7789</span></div><div class="line">tcp 0 0 192.168.137.222:42345 192.168.137.225:7789 ESTABLISHED - </div><div class="line">tcp 0 0 192.168.137.222:7789 192.168.137.225:42325 ESTABLISHED -</div><div class="line"><span class="comment">######查看DRBD启动状态</span></div><div class="line">[root@nod2 ~]<span class="comment"># drbd-overview</span></div><div class="line"> 0:drbd/0  Connected Secondary/Secondary Inconsistent/Inconsistent C r-----</div></pre></td></tr></table></figure>
<h3 id="7-资源的连接状态详细介绍"><a href="#7-资源的连接状态详细介绍" class="headerlink" title="7.资源的连接状态详细介绍"></a>7.资源的连接状态详细介绍</h3><h4 id="如何查看资源连接状态？"><a href="#如何查看资源连接状态？" class="headerlink" title="如何查看资源连接状态？"></a>如何查看资源连接状态？</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@nod1 ~]<span class="comment"># drbdadm cstate drbd #drbd为资源名称</span></div><div class="line">Connected</div></pre></td></tr></table></figure>
<h4 id="资源的连接状态；一个资源可能有以下连接状态中的一种"><a href="#资源的连接状态；一个资源可能有以下连接状态中的一种" class="headerlink" title="资源的连接状态；一个资源可能有以下连接状态中的一种"></a>资源的连接状态；一个资源可能有以下连接状态中的一种</h4><ul>
<li>StandAlone 独立的：网络配置不可用；资源还没有被连接或是被管理断开（使用 drbdadm disconnect 命令），或是由于出现认证失败或是脑裂的情况</li>
<li>Disconnecting 断开：断开只是临时状态，下一个状态是StandAlone独立的</li>
<li>Unconnected 悬空：是尝试连接前的临时状态，可能下一个状态为WFconnection和WFReportParams</li>
<li>Timeout 超时：与对等节点连接超时，也是临时状态，下一个状态为Unconected悬空</li>
<li>BrokerPipe：与对等节点连接丢失，也是临时状态，下一个状态为Unconected悬空</li>
<li>NetworkFailure：与对等节点推动连接后的临时状态，下一个状态为Unconected悬空</li>
<li>ProtocolError：与对等节点推动连接后的临时状态，下一个状态为Unconected悬空</li>
<li>TearDown 拆解：临时状态，对等节点关闭，下一个状态为Unconected悬空</li>
<li>WFConnection：等待和对等节点建立网络连接</li>
<li>WFReportParams：已经建立TCP连接，本节点等待从对等节点传来的第一个网络包</li>
<li>Connected 连接：DRBD已经建立连接，数据镜像现在可用，节点处于正常状态</li>
<li>StartingSyncS：完全同步，有管理员发起的刚刚开始同步，未来可能的状态为SyncSource或PausedSyncS</li>
<li>StartingSyncT：完全同步，有管理员发起的刚刚开始同步，下一状态为WFSyncUUID</li>
<li>WFBitMapS：部分同步刚刚开始，下一步可能的状态为SyncSource或PausedSyncS</li>
<li>WFBitMapT：部分同步刚刚开始，下一步可能的状态为WFSyncUUID</li>
<li>WFSyncUUID：同步即将开始，下一步可能的状态为SyncTarget或PausedSyncT</li>
<li>SyncSource：以本节点为同步源的同步正在进行</li>
<li>SyncTarget：以本节点为同步目标的同步正在进行</li>
<li>PausedSyncS：以本地节点是一个持续同步的源，但是目前同步已经暂停，可能是因为另外一个同步正在进行或是使用命令(drbdadm pause-sync)暂停了同步</li>
<li>PausedSyncT：以本地节点为持续同步的目标，但是目前同步已经暂停，这可以是因为另外一个同步正在进行或是使用命令(drbdadm pause-sync)暂停了同步</li>
<li>VerifyS：以本地节点为验证源的线上设备验证正在执行</li>
<li>VerifyT：以本地节点为验证目标的线上设备验证正在执行</li>
</ul>
<h4 id="资源角色"><a href="#资源角色" class="headerlink" title="资源角色"></a>资源角色</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看资源角色命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[root@nod1 ~]<span class="comment"># drbdadm role drbd</span></div><div class="line">Secondary/Secondary</div><div class="line">[root@nod1 ~]<span class="comment"># cat /proc/drbd</span></div><div class="line">version: 8.4.3 (api:1/proto:86-101)</div><div class="line">GIT-hash: 89a294209144b68adb3ee85a73221f964d3ee515 build by gardner@, 2013-05-27 04:30:21</div><div class="line"> 0: cs:Connected ro:Secondary/Secondary ds:Inconsistent/Inconsistent C r-----</div><div class="line"> ns:0 nr:0 dw:0 dr:0 al:0 bm:0 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:2103412</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注释：</p>
<ul>
<li>Parimary 主：资源目前为主，并且可能正在被读取或写入，如果不是双主只会出现在两个节点中的其中一个节点上</li>
<li>Secondary 次：资源目前为次，正常接收对等节点的更新</li>
<li>Unknown 未知：资源角色目前未知，本地的资源不会出现这种状态</li>
</ul>
<h4 id="硬盘状态"><a href="#硬盘状态" class="headerlink" title="硬盘状态"></a>硬盘状态</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;查看硬盘状态命令</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@nod1 ~]<span class="comment"># drbdadm dstate drbd</span></div><div class="line">Inconsistent/Inconsistent</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;本地和对等节点的硬盘有可能为下列状态之一：</p>
<ul>
<li>Diskless 无盘：本地没有块设备分配给DRBD使用，这表示没有可用的设备，或者使用drbdadm命令手工分离或是底层的I/O错误导致自动分离</li>
<li>Attaching：读取无数据时候的瞬间状态</li>
<li>Failed 失败：本地块设备报告I/O错误的下一个状态，其下一个状态为Diskless无盘</li>
<li>Negotiating：在已经连接的DRBD设置进行Attach读取无数据前的瞬间状态</li>
<li>Inconsistent：数据是不一致的，在两个节点上（初始的完全同步前）这种状态出现后立即创建一个新的资源。此外，在同步期间（同步目标）在一个节点上出现这种状态</li>
<li>Outdated：数据资源是一致的，但是已经过时</li>
<li>DUnknown：当对等节点网络连接不可用时出现这种状态</li>
<li>Consistent：一个没有连接的节点数据一致，当建立连接时，它决定数据是UpToDate或是Outdated</li>
<li>UpToDate：一致的最新的数据状态，这个状态为正常状态</li>
</ul>
<h4 id="启用和禁用资源"><a href="#启用和禁用资源" class="headerlink" title="启用和禁用资源"></a>启用和禁用资源</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="comment">######手动启用资源</span></div><div class="line">drbdadm up &lt;resource&gt;</div><div class="line"><span class="comment">######手动禁用资源</span></div><div class="line">drbdadm down &lt;resource&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注释：</p>
<ul>
<li>resource：为资源名称；当然也可以使用all表示[停用|启用]所有资源</li>
</ul>
<h4 id="升级和降级资源"><a href="#升级和降级资源" class="headerlink" title="升级和降级资源"></a>升级和降级资源</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="comment">######升级资源</span></div><div class="line">drbdadm primary &lt;resource&gt;</div><div class="line"><span class="comment">######降级资源</span></div><div class="line">drbdadm secondary &lt;resource&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注释：在单主模式下的DRBD，两个节点同时处于连接状态，任何一个节点都可以在特定的时间内变成主；但两个节点中只能一为主，如果已经有一个主，需先降级才可能升级；在双主模式下没有这个限制</p>
<h3 id="8-初始化设备同步"><a href="#8-初始化设备同步" class="headerlink" title="8.初始化设备同步"></a>8.初始化设备同步</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;选择一个初始同步源；如果是新初始化的或是空盘，这个选择可以是任意的，但是如果其中的一个节点已经在使用并包含有用的数据，那么选择同步源是至关重要的；如果选错了初始化同步方向，就会造成数据丢失，因此需要十分小心</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;启动初始化完全同步，这一步只能在初始化资源配置的一个节点上进行，并作为同步源选择的节点上；命令如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div></pre></td><td class="code"><pre><div class="line">[root@nod1 ~]<span class="comment"># drbdadm -- --overwrite-data-of-peer primary drbd</span></div><div class="line">[root@nod1 ~]<span class="comment"># cat /proc/drbd #查看同步进度</span></div><div class="line">version: 8.4.3 (api:1/proto:86-101)</div><div class="line">GIT-hash: 89a294209144b68adb3ee85a73221f964d3ee515 build by gardner@, 2013-05-27 04:30:21</div><div class="line"> 0: cs:SyncSource ro:Primary/Secondary ds:UpToDate/Inconsistent C r---n-</div><div class="line"> ns:1897624 nr:0 dw:0 dr:1901216 al:0 bm:115 lo:0 pe:3 ua:3 ap:0 ep:1 wo:f oos:207988</div><div class="line"> [=================&gt;..] sync<span class="string">'ed: 90.3% (207988/2103412)K</span></div><div class="line"><span class="string"> finish: 0:00:07 speed: 26,792 (27,076) K/sec</span></div><div class="line"><span class="string">######当同步完成时如以下状态</span></div><div class="line"><span class="string">version: 8.4.3 (api:1/proto:86-101)</span></div><div class="line"><span class="string">GIT-hash: 89a294209144b68adb3ee85a73221f964d3ee515 build by gardner@, 2013-05-27 04:30:21</span></div><div class="line"><span class="string"> 0: cs:Connected ro:Primary/Secondary ds:UpToDate/UpToDate C r-----</span></div><div class="line"><span class="string"> ns:2103412 nr:0 dw:0 dr:2104084 al:0 bm:129 lo:0 pe:0 ua:0 ap:0 ep:1 wo:f oos:0</span></div><div class="line"><span class="string">注释： drbd：为资源名称</span></div><div class="line"><span class="string">######查看同步进度也可使用以下命令</span></div><div class="line"><span class="string">drbd-overview</span></div></pre></td></tr></table></figure>
<h3 id="9-创建文件系统"><a href="#9-创建文件系统" class="headerlink" title="9.创建文件系统"></a>9.创建文件系统</h3><h4 id="文件系统只能挂载在主-Primary-节点上，因此在设置好主节点后才可以对DRBD设备进行格式化操作"><a href="#文件系统只能挂载在主-Primary-节点上，因此在设置好主节点后才可以对DRBD设备进行格式化操作" class="headerlink" title="文件系统只能挂载在主(Primary)节点上，因此在设置好主节点后才可以对DRBD设备进行格式化操作"></a>文件系统只能挂载在主(Primary)节点上，因此在设置好主节点后才可以对DRBD设备进行格式化操作</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="comment">######格式化文件系统</span></div><div class="line">[root@nod1 ~]<span class="comment"># mkfs.ext4 /dev/drbd0</span></div><div class="line"><span class="comment">######挂载文件系统</span></div><div class="line">[root@nod1 ~]<span class="comment"># mount /dev/drbd0 /mnt/</span></div><div class="line"><span class="comment">######查看挂载</span></div><div class="line">[root@nod1 ~]<span class="comment"># mount |grep drbd0</span></div><div class="line">/dev/drbd0 on /mnt <span class="built_in">type</span> ext4 (rw)</div><div class="line">注释：</div><div class="line"><span class="string">"/dev/drbd0"</span>为资源中定义已定义的资源名称</div><div class="line"><span class="comment">######查看DRBD状态</span></div><div class="line">[root@nod1 ~]<span class="comment"># drbd-overview</span></div><div class="line"> 0:drbd/0  Connected Primary/Secondary UpToDate/UpToDate C r-----</div><div class="line">注释：</div><div class="line">Primary：当前节点为主；在前面为当前节点</div><div class="line">Secondary：备用节点为次</div></pre></td></tr></table></figure>
<h4 id="在挂载目录中创建一个测试文件并卸载；然后"><a href="#在挂载目录中创建一个测试文件并卸载；然后" class="headerlink" title="在挂载目录中创建一个测试文件并卸载；然后"></a>在挂载目录中创建一个测试文件并卸载；然后</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@nod1 ~]<span class="comment"># mkdir /mnt/test</span></div><div class="line">[root@nod1 ~]<span class="comment"># ls /mnt/</span></div><div class="line">lost+found  <span class="built_in">test</span></div><div class="line"><span class="comment">######在切换主节点时必须保证资源不在使用</span></div><div class="line">[root@nod1 ~]<span class="comment"># umount /mnt/</span></div></pre></td></tr></table></figure>
<h4 id="切换主备节点"><a href="#切换主备节点" class="headerlink" title="切换主备节点"></a>切换主备节点</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line"><span class="comment">######先把当前主节点降级为次</span></div><div class="line">[root@nod1 ~]<span class="comment"># drbdadm secondary drbd</span></div><div class="line"><span class="comment">######查看DRBD状态</span></div><div class="line">[root@nod1 ~]<span class="comment"># drbd-overview</span></div><div class="line"> 0:drbd/0  Connected Secondary/Secondary UpToDate/UpToDate C r-----</div><div class="line"><span class="comment">######在NOD2节点升级</span></div><div class="line">[root@nod2 ~]<span class="comment"># drbdadm primary drbd</span></div><div class="line"><span class="comment">######查看DRBD状态</span></div><div class="line">[root@nod2 ~]<span class="comment"># drbd-overview</span></div><div class="line"> 0:drbd/0  Connected Primary/Secondary UpToDate/UpToDate C r-----</div></pre></td></tr></table></figure>
<h4 id="挂载设备并验证文件是否存在"><a href="#挂载设备并验证文件是否存在" class="headerlink" title="挂载设备并验证文件是否存在"></a>挂载设备并验证文件是否存在</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@nod2 ~]<span class="comment"># mount /dev/drbd0 /mnt/</span></div><div class="line">[root@nod2 ~]<span class="comment"># ls /mnt/</span></div><div class="line">lost+found  <span class="built_in">test</span></div></pre></td></tr></table></figure>
<h2 id="七、DRBD脑裂的模拟及修复"><a href="#七、DRBD脑裂的模拟及修复" class="headerlink" title="七、DRBD脑裂的模拟及修复"></a>七、DRBD脑裂的模拟及修复</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注释：我们还接着上面的实验继续进行，现在NOD2为主节点而NOD1为备节点</p>
<h3 id="1。断开主-parmary-节点"><a href="#1。断开主-parmary-节点" class="headerlink" title="1。断开主(parmary)节点"></a>1。断开主(parmary)节点</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;关机、断开网络或重新配置其他的IP都可以；这里选择的是断开网络</p>
<h3 id="2-查看两节点状态"><a href="#2-查看两节点状态" class="headerlink" title="2.查看两节点状态"></a>2.查看两节点状态</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@nod2 ~]<span class="comment"># drbd-overview</span></div><div class="line"> 0:drbd/0  WFConnection Primary/Unknown UpToDate/DUnknown C r----- /mnt ext4 2.0G 68M 1.9G 4%</div><div class="line">[root@nod1 ~]<span class="comment"># drbd-overview</span></div><div class="line"> 0:drbd/0  StandAlone Secondary/Unknown UpToDate/DUnknown r-----</div><div class="line"><span class="comment">######由上可以看到两个节点已经无法通信；NOD2为主节点，NOD1为备节点</span></div></pre></td></tr></table></figure>
<h3 id="3-将NOD1节点升级为主-primary-节点并挂载资源"><a href="#3-将NOD1节点升级为主-primary-节点并挂载资源" class="headerlink" title="3.将NOD1节点升级为主(primary)节点并挂载资源"></a>3.将NOD1节点升级为主(primary)节点并挂载资源</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">[root@nod1 ~]<span class="comment"># drbdadm primary drbd</span></div><div class="line">[root@nod1 ~]<span class="comment"># drbd-overview</span></div><div class="line"> 0:drbd/0  StandAlone Primary/Unknown UpToDate/DUnknown r-----</div><div class="line">[root@nod1 ~]<span class="comment"># mount /dev/drbd0 /mnt/</span></div><div class="line">[root@nod1 ~]<span class="comment"># mount | grep drbd0</span></div><div class="line">/dev/drbd0 on /mnt <span class="built_in">type</span> ext4 (rw)</div></pre></td></tr></table></figure>
<h3 id="4-假如原来的主-primary-节点修复好重新上线了，这时出现了脑裂情况"><a href="#4-假如原来的主-primary-节点修复好重新上线了，这时出现了脑裂情况" class="headerlink" title="4.假如原来的主(primary)节点修复好重新上线了，这时出现了脑裂情况"></a>4.假如原来的主(primary)节点修复好重新上线了，这时出现了脑裂情况</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">[root@nod2 ~]<span class="comment"># tail -f /var/log/messages</span></div><div class="line">Sep 19 01:56:06 nod2 kernel: d-con drbd: Terminating drbd_a_drbd</div><div class="line">Sep 19 01:56:06 nod2 kernel: block drbd0: helper <span class="built_in">command</span>: /sbin/drbdadm initial-split-brain minor-0 <span class="built_in">exit</span> code 0 (0x0)</div><div class="line">Sep 19 01:56:06 nod2 kernel: block drbd0: Split-Brain detected but unresolved, dropping connection!</div><div class="line">Sep 19 01:56:06 nod2 kernel: block drbd0: helper <span class="built_in">command</span>: /sbin/drbdadm split-brain minor-0</div><div class="line">Sep 19 01:56:06 nod2 kernel: block drbd0: helper <span class="built_in">command</span>: /sbin/drbdadm split-brain minor-0 <span class="built_in">exit</span> code 0 (0x0)</div><div class="line">Sep 19 01:56:06 nod2 kernel: d-con drbd: conn( NetworkFailure -&gt; Disconnecting )</div><div class="line">Sep 19 01:56:06 nod2 kernel: d-con drbd: error receiving ReportState, e: -5 l: 0!</div><div class="line">Sep 19 01:56:06 nod2 kernel: d-con drbd: Connection closed</div><div class="line">Sep 19 01:56:06 nod2 kernel: d-con drbd: conn( Disconnecting -&gt; StandAlone )</div><div class="line">Sep 19 01:56:06 nod2 kernel: d-con drbd: receiver terminated</div><div class="line">Sep 19 01:56:06 nod2 kernel: d-con drbd: Terminating drbd_r_drbd</div><div class="line">Sep 19 01:56:18 nod2 kernel: block drbd0: role( Primary -&gt; Secondary )</div></pre></td></tr></table></figure>
<h3 id="5-再次查看两节点的状态"><a href="#5-再次查看两节点的状态" class="headerlink" title="5.再次查看两节点的状态"></a>5.再次查看两节点的状态</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@nod1 ~]<span class="comment"># drbdadm role drbd</span></div><div class="line">Primary/Unknown</div><div class="line">[root@nod2 ~]<span class="comment"># drbdadm role drbd</span></div><div class="line">Primary/Unknown</div></pre></td></tr></table></figure>
<h3 id="6-查看NOD1与NOD2连接状态"><a href="#6-查看NOD1与NOD2连接状态" class="headerlink" title="6.查看NOD1与NOD2连接状态"></a>6.查看NOD1与NOD2连接状态</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@nod1 ~]<span class="comment"># drbd-overview</span></div><div class="line"> 0:drbd/0  StandAlone Primary/Unknown UpToDate/DUnknown r----- /mnt ext4 2.0G 68M 1.9G 4%</div><div class="line">[root@nod2 ~]<span class="comment"># drbd-overview</span></div><div class="line"> 0:drbd/0  WFConnection Primary/Unknown UpToDate/DUnknown C r----- /mnt ext4 2.0G 68M 1.9G 4%</div><div class="line"><span class="comment">######由上可见，状态为StandAlone时，主备节点是不会通信的</span></div></pre></td></tr></table></figure>
<h3 id="7-查看DRBD的服务状态"><a href="#7-查看DRBD的服务状态" class="headerlink" title="7.查看DRBD的服务状态"></a>7.查看DRBD的服务状态</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line">[root@nod1 ~]<span class="comment"># service drbd status</span></div><div class="line">drbd driver loaded OK; device status:</div><div class="line">version: 8.4.3 (api:1/proto:86-101)</div><div class="line">GIT-hash: 89a294209144b68adb3ee85a73221f964d3ee515 build by gardner@, 2013-05-27 04:30:21</div><div class="line">m:res cs ro ds p mounted fstype</div><div class="line">0:drbd StandAlone Primary/Unknown  UpToDate/DUnknown  r----- ext4</div><div class="line">[root@nod2 ~]<span class="comment"># service drbd status</span></div><div class="line">drbd driver loaded OK; device status:</div><div class="line">version: 8.4.3 (api:1/proto:86-101)</div><div class="line">GIT-hash: 89a294209144b68adb3ee85a73221f964d3ee515 build by gardner@, 2013-05-27 04:30:21</div><div class="line">m:res cs ro ds p mounted fstype</div><div class="line">0:drbd WFConnection Primary/Unknown  UpToDate/DUnknown  C  /mnt  ext4</div></pre></td></tr></table></figure>
<h3 id="8-在NOD1备用节点处理办法"><a href="#8-在NOD1备用节点处理办法" class="headerlink" title="8.在NOD1备用节点处理办法"></a>8.在NOD1备用节点处理办法</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">[root@nod1 ~]<span class="comment"># umount /mnt/</span></div><div class="line">[root@nod1 ~]<span class="comment"># drbdadm disconnect drbd</span></div><div class="line">drbd: Failure: (162) Invalid configuration request</div><div class="line">additional info from kernel:</div><div class="line">unknown connection</div><div class="line">Command <span class="string">'drbdsetup disconnect ipv4:192.168.137.225:7789 ipv4:192.168.137.222:7789'</span> terminated with <span class="built_in">exit</span> code 10</div><div class="line">[root@nod1 ~]<span class="comment"># drbdadm secondary drbd</span></div><div class="line">[root@nod1 ~]<span class="comment"># drbd-overview</span></div><div class="line"> 0:drbd/0  StandAlone Secondary/Unknown UpToDate/DUnknown r-----</div><div class="line">[root@nod1 ~]<span class="comment"># drbdadm connect --discard-my-data drbd</span></div><div class="line"><span class="comment">######执行完以上三步后，你查看会发现还是不可用</span></div><div class="line">[root@nod1 ~]<span class="comment"># drbd-overview</span></div><div class="line"> 0:drbd/0  WFConnection Secondary/Unknown UpToDate/DUnknown C r-----</div></pre></td></tr></table></figure>
<h3 id="9-需要在NOD2节点上重新建立连接资源"><a href="#9-需要在NOD2节点上重新建立连接资源" class="headerlink" title="9.需要在NOD2节点上重新建立连接资源"></a>9.需要在NOD2节点上重新建立连接资源</h3><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[root@nod2 ~]<span class="comment"># drbdadm connect drbd</span></div><div class="line"><span class="comment">######查看节点连接状态</span></div><div class="line">[root@nod2 ~]<span class="comment"># drbd-overview</span></div><div class="line"> 0:drbd/0  Connected Primary/Secondary UpToDate/UpToDate C r----- /mnt ext4 2.0G 68M 1.9G 4%</div><div class="line">[root@nod1 ~]<span class="comment"># drbd-overview</span></div><div class="line"> 0:drbd/0  Connected Secondary/Primary UpToDate/UpToDate C r-----</div><div class="line"><span class="comment">######由上可见已经恢复到正常运行状态</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>注意</strong>：特别提醒，如果是单主模式，资源只能在主(Primary)节点上挂载使用，而且不建议手动切换主备节点</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>到此DRBD的安装配置及故障修复已结束，DRBD的双主模式一般情况不会用到，这里也不再介绍双主模式的配置</strong></p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/HA集群/3. heartbeat和keepalived的区别/">heartbeat和keepalived的区别</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/HA集群/">HA集群</a>
			</span>
		
	</div>

	

	
		<h2 id="1-Keepalived使用更简单"><a href="#1-Keepalived使用更简单" class="headerlink" title="1.Keepalived使用更简单"></a>1.Keepalived使用更简单</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;从安装、配置、使用、维护等角度上对比，Keepalived都比Heartbeat要简单得多，尤其是Heartbeat 2.1.4后拆分成3个子项目，安装、配置、使用都比较复杂，尤其是出问题的时候，都不知道具体是哪个子系统出问题了；而Keepalived只有1个安装文件、1个配置文件，配置文件也简单很多；</p>
<h2 id="2-Heartbeat功能更强大"><a href="#2-Heartbeat功能更强大" class="headerlink" title="2.Heartbeat功能更强大"></a>2.Heartbeat功能更强大</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Heartbeat虽然复杂，但功能更强大，配套工具更全，<code>适合做大型集群管理</code>，而Keepalived主要用于<code>集群倒换</code>，基本没有管理功能；</p>
<h2 id="3-协议不同"><a href="#3-协议不同" class="headerlink" title="3.协议不同"></a>3.协议不同</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;Keepalived使用VRRP协议进行通信和选举，Heartbeat使用心跳进行通信和选举；Heartbeat除了走网络外，还可以通过串口通信，貌似更可靠；</p>
<h2 id="4-使用方式基本类似"><a href="#4-使用方式基本类似" class="headerlink" title="4.使用方式基本类似"></a>4.使用方式基本类似</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果要基于两者设计高可用方案，最终都要<code>根据业务需要写自定义的脚本</code>，Keepalived的脚本没有任何约束，随便怎么写都可以；Heartbeat的脚本有约束，即要支持service start/stop/restart这种方式，而且Heartbeart提供了很多默认脚本，简单的绑定ip，启动apache等操作都已经有了；</p>
<h2 id="使用建议"><a href="#使用建议" class="headerlink" title="使用建议"></a>使用建议</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>优先使用Keepalived</code>，当Keepalived不够用的时候才选择Heartbeat</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/HA集群/2. heartbeat 配置文件详解/">heartbeat 部署过程</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/HA集群/">HA集群</a>
			</span>
		
	</div>

	

	
		<h2 id="1-配置ha-cf"><a href="#1-配置ha-cf" class="headerlink" title="1 配置ha.cf"></a>1 配置ha.cf</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;第一个是ha.cf该文件位于在安装后创建的/etc/ha.d目录中。该文件中包括为Heartbeat使用何种介质通路和如何配置他们的信息。在源代码目录中的ha.cf文件包含了您可以使用的全部选项，详述如下：</p>
<h3 id="serial-dev-ttyS0"><a href="#serial-dev-ttyS0" class="headerlink" title="serial /dev/ttyS0"></a>serial /dev/ttyS0</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用串口heartbeat－如果不使用串口heartbeat，则必须使用其他的介质，如bcast（以太网）heartbeat。用适当的设备文件代替/dev/ttyS0。</p>
<h3 id="watchdog-dev-watchdog"><a href="#watchdog-dev-watchdog" class="headerlink" title="watchdog /dev/watchdog"></a>watchdog /dev/watchdog</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;该选项是可选配置。通过Watchdog 功能可以获得提供最少功能的系统，该系统不提供heartbeat，可以在持续一份钟的不正常状态后重新启动。该功能有助于避免一台机器在被认定已经死亡之后恢复heartbeat的情况。如果这种情况发生并且磁盘挂载因故障而迁移（fail over），便有可能有两个节点同时挂载一块磁盘。如果要使用这项功能，则除了这行之外，也需要加载“softdog”内核模块，并创建相应的设备文件。方法是使用命令“insmod softdog”加载模块。然后输入“grep misc /proc/devices”并记住得到的数字（应该是10）。然后输入”cat /proc/misc | grep watchdog”并记住输出的数字（应该是130）。根据以上得到的信息可以创建设备文件，“mknod /dev/watchdog c 10 130”。</p>
<h3 id="bcast-eth1"><a href="#bcast-eth1" class="headerlink" title="bcast eth1"></a>bcast eth1</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;表示在eth1接口上使用广播heartbeat（将eth1替换为eth0，eth2，或者您使用的任何接口）。</p>
<h3 id="mcast-eth1-225-0-0-1-694-1-0"><a href="#mcast-eth1-225-0-0-1-694-1-0" class="headerlink" title="mcast eth1 225.0.0.1 694 1 0"></a>mcast eth1 225.0.0.1 694 1 0</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果采用组播通讯，在这里可以设置组播通讯所使用的接口，绑定的组播ip地址（在224.0.0.0-239.255.255.255之间），通讯端口，ttl(time to live)所能经过路由的跳数，是否允许回环（也就是本地发出的数据包是否还接收）。</p>
<h3 id="ucast-eth1-10-0-0-1"><a href="#ucast-eth1-10-0-0-1" class="headerlink" title="ucast eth1 10.0.0.1"></a>ucast eth1 10.0.0.1</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果采用单播，可以配置其网络接口及其所使用的ip地址。</p>
<h3 id="keepalive-2"><a href="#keepalive-2" class="headerlink" title="keepalive 2"></a>keepalive 2</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;设定heartbeat之间的时间间隔为2秒。</p>
<h3 id="warntime-10"><a href="#warntime-10" class="headerlink" title="warntime 10"></a>warntime 10</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在日志中发出“late heartbeat“警告之前等待的时间，单位为秒。</p>
<h3 id="deadtime-30"><a href="#deadtime-30" class="headerlink" title="deadtime 30"></a>deadtime 30</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在30秒后宣布节点死亡。</p>
<h3 id="initdead-120"><a href="#initdead-120" class="headerlink" title="initdead 120"></a>initdead 120</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在某些配置下，重启后网络需要一些时间才能正常工作。这个单独的”deadtime”选项可以处理这种情况。它的取值至少应该为通常deadtime的两倍。</p>
<h3 id="baud-19200"><a href="#baud-19200" class="headerlink" title="baud 19200"></a>baud 19200</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;波特率，串口通信的速度。</p>
<h3 id="udpport-694"><a href="#udpport-694" class="headerlink" title="udpport 694"></a>udpport 694</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用端口694进行bcast和ucast通信。这是默认的，并且在IANA官方注册的端口号。</p>
<h3 id="auto-failback-on"><a href="#auto-failback-on" class="headerlink" title="auto_failback on"></a>auto_failback on</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;该选项是必须配置的。对于那些熟悉Tru64 Unix的人来说，heartbeat的工作方式类似于“favored member“模式。在failover之前，haresources文件中列出的主节点掌握所有的资源，之后从节点接管这些资源。当auto_failback设置为on时，一旦主节点重新恢复联机，将从从节点取回所有资源。若该选项设置为off，主节点便不能重新获得资源。该选项与废弃的nice_failback选项类似。如果要从一个nice_failback设置为off的集群升级到这个或更新的版本，需要特别注意一些事项以防止flash cut。请参阅FAQ中关于如何处理这类情况的章节。</p>
<h3 id="node-primary-mydomain-com"><a href="#node-primary-mydomain-com" class="headerlink" title="node primary.mydomain.com"></a>node primary.mydomain.com</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;该选项是必须配置的。集群中机器的主机名，与“uname –n”的输出相同。</p>
<h3 id="node-backup-mydomain-com"><a href="#node-backup-mydomain-com" class="headerlink" title="node backup.mydomain.com"></a>node backup.mydomain.com</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;该选项是必须配置的。同上。</p>
<h3 id="respawn"><a href="#respawn" class="headerlink" title="respawn"></a>respawn</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;该选项是可选配置的：列出将要执行和监控的命令。例如：要执行ccm守护进程，则要添加如下的内容：</p>
<h3 id="respawn-hacluster-usr-lib-heartbeat-ccm"><a href="#respawn-hacluster-usr-lib-heartbeat-ccm" class="headerlink" title="respawn hacluster /usr/lib/heartbeat/ccm"></a>respawn hacluster /usr/lib/heartbeat/ccm</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使得Heartbeat以userid（在本例中为hacluster）的身份来执行该进程并监视该进程的执行情况，如果其死亡便重启之。对于ipfail，则应该是：</p>
<h3 id="respawn-hacluster-usr-lib-heartbeat-ipfail"><a href="#respawn-hacluster-usr-lib-heartbeat-ipfail" class="headerlink" title="respawn hacluster /usr/lib/heartbeat/ipfail"></a>respawn hacluster /usr/lib/heartbeat/ipfail</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;对于pingd则应该是：</p>
<h3 id="respawn-hacluster-usr-lib64-heartbeat-pingd-m-100-d-5s"><a href="#respawn-hacluster-usr-lib64-heartbeat-pingd-m-100-d-5s" class="headerlink" title="respawn hacluster /usr/lib64/heartbeat/pingd -m 100 -d 5s"></a>respawn hacluster /usr/lib64/heartbeat/pingd -m 100 -d 5s</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意：如果结束进程的退出代码为100，则不会重启该进程。</p>
<h3 id="apiauth-pingd-gid-haclient-uid-hacluster"><a href="#apiauth-pingd-gid-haclient-uid-hacluster" class="headerlink" title="apiauth pingd gid=haclient uid=hacluster"></a>apiauth pingd gid=haclient uid=hacluster</h3><h2 id="2-配置haresources"><a href="#2-配置haresources" class="headerlink" title="2 配置haresources"></a>2 配置haresources</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置好ha.cf文件之后，便是haresources文件。该文件列出集群所提供的服务以及服务的默认所有者。 注意：两个集群节点上的该文件必须相同。集群的IP地址是该选项是必须配置的，不能在haresources文件以外配置该地址, haresources文件用于指定双机系统的主节点、集群IP、子网掩码、广播地址以及启动的服务等。其配置语句格式如下：</p>
<p>node-name network-config</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;其中node-name指定双机系统的主节点，取值必须匹配ha.cf文件中node选项设置的主机名中的一个，node选项设置的另一个主机名成为从节点。network-config用于网络设置，包括指定集群IP、子网掩码、广播地址等。resource-group用于设置heartbeat启动的服务，该服务最终由双机系统通过集群IP对外提供。在本文中我们假设要配置的HA服务为Apache和Samba。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在haresources文件中需要如下内容：</p>
<p>primary.mydomain.com 192.168.85.3 httpd smb</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;该行指定在启动时，节点linuxha1得到IP地址192.168.85.3，并启动Apache和Samba。在停止时，Heartbeat将首先停止smb，然后停止Apache，最后释放IP地址192.168.85.3。这里假设命令“uname –n”的输出为“primary.mydomain.com”－如果输出为“primary”，便应使用“primary”。</p>
<p>primary.mydomain.com IPaddr::192.168.21.107/24/eth0 drbddisk::r0 Filesystem::/dev/drbd1::/data::ext4 nfs</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;正确配置好haresources文件之后，将ha.cf和haresource拷贝到/etc/ha.d目录。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意：资源文件中能执行的命令必须在/etc/ha.d/resource.d/ 中可见</p>
<h2 id="3-配置Authkeys"><a href="#3-配置Authkeys" class="headerlink" title="3 配置Authkeys"></a>3 配置Authkeys</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;需要配置的第三个文件authkeys决定了您的认证密钥。共有三种认证方式：crc，md5，和sha1。您可能会问：“我应该用哪个方法呢？”简而言之： 如果您的Heartbeat运行于安全网络之上，如本例中的交叉线，可以使用crc，从资源的角度来看，这是代价最低的方法。如果网络并不安全，但您也希望降低CPU使用，则使用md5。最后，如果您想得到最好的认证，而不考虑CPU使用情况，则使用sha1，它在三者之中最难破解。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;文件格式如下：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">auth []</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;因此，对于sha1，示例的/etc/ha.d/authkeys可能是</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">auth 1</div><div class="line">1 sha1 key-for-sha1-any-text-you-want</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;对于md5，只要将上面内容中的sha1换成md5就可以了。 对于crc，可作如下配置：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">auth 2</div><div class="line">2 crc</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;不论您在关键字auth后面指定的是什么索引值，在后面必须要作为键值再次出现。如果您指定“auth 4”，则在后面一定要有一行的内容为“4 ”。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;确保该文件的访问权限是安全的，如600。</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/HA集群/1. HA 集群配置/">HA 集群配置</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/HA集群/">HA集群</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;HA 即 （high available）高可用，又被叫做双机热备，用于关键性业务。 简单理解就是，有两台机器A和B，正常是A提供服务，B待命闲置，当A宕机或服务宕掉，会切换至B机器继续提供服务。常用实现高可用的开源软件有heartbeat和keepalived，其中keepalived有负载均衡的功能。</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/heartbeat%E9%83%A8%E7%BD%B2%E8%BF%87%E7%A8%8B/01.jpeg?raw=true" alt="images"><figcaption class="figure__caption">images</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如图所示为一个 HA 结构，一个交换机下面有两台机器 web1 和 web2 ，其中 web1 为主节点，正常是它在提供服务，而 web2 备用节点是闲置的。 web1 和 web2 中间有一根心跳线，检查对方的存活状态。流动 IP ，也叫 vip 是对外提供服务的 ip ，正常情况下，是配置在 web1 上的，当 web1 宕机后， web2 会自动配置该 vip ，对外提供服务。</p>
<h2 id="heartbeat-部署"><a href="#heartbeat-部署" class="headerlink" title="heartbeat 部署"></a>heartbeat 部署</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;下面使用 heartbeat 来做 HA 集群，并且把 nginx 服务作为 HA 对应的服务</p>
<h3 id="准备工作"><a href="#准备工作" class="headerlink" title="准备工作"></a>准备工作</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;两台机器，都是 centos6.5，网卡 eth0 ip 地址为</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">master 192.168.0.69</div><div class="line">slave 192.168.0.68</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;eth1 ip 地址为</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">master 192.168.91.100</div><div class="line">slave 192.168.91.101</div></pre></td></tr></table></figure>
<h4 id="1-hostname-设置好，分别为-master-和-slave"><a href="#1-hostname-设置好，分别为-master-和-slave" class="headerlink" title="1. hostname 设置好，分别为 master 和 slave"></a>1. hostname 设置好，分别为 master 和 slave</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主上设置 hostname</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@HA1 ~]<span class="comment"># hostname master</span></div><div class="line">[root@HA1 ~]<span class="comment"># bash</span></div><div class="line">[root@master ~]<span class="comment"># vim /etc/sysconfig/network</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">NETWORKING=yes</div><div class="line">HOSTNAME=master</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;从上设置 hostname</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@HA2 ~]<span class="comment"># hostname slave</span></div><div class="line">[root@HA2 ~]<span class="comment"># bash</span></div><div class="line">[root@slave ~]<span class="comment"># vim /etc/sysconfig/network</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">NETWORKING=yes</div><div class="line">HOSTNAME=master</div></pre></td></tr></table></figure>
<h4 id="2-关闭防火墙"><a href="#2-关闭防火墙" class="headerlink" title="2.关闭防火墙"></a>2.关闭防火墙</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主和从上都操作</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">iptables -F</div><div class="line">service iptables save</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主和从都关闭 selinux</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">setenforce 0</div><div class="line">sed -i <span class="string">'s/SELINUX=enforcing/SELINUX=disabled/'</span> /etc/selinux/config</div></pre></td></tr></table></figure>
<h4 id="3-配置-hosts"><a href="#3-配置-hosts" class="headerlink" title="3.配置 hosts"></a>3.配置 hosts</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主和从都编辑</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vim /etc/hosts</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;增加内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">192.168.0.69 master</div><div class="line">192.168.0.68 slave</div></pre></td></tr></table></figure>
<h4 id="4-安装-epel-扩展源"><a href="#4-安装-epel-扩展源" class="headerlink" title="4.安装 epel 扩展源"></a>4.安装 epel 扩展源</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主和从都执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yum install -y epel-release</div></pre></td></tr></table></figure>
<h4 id="5-安装-heartbeat"><a href="#5-安装-heartbeat" class="headerlink" title="5.安装 heartbeat"></a>5.安装 heartbeat</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主和从都执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yum install -y heartbeat* libnet nginx</div></pre></td></tr></table></figure>
<h4 id="6-主上（master）配置"><a href="#6-主上（master）配置" class="headerlink" title="6.主上（master）配置"></a>6.主上（master）配置</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># cd /usr/share/doc/heartbeat-3.0.4/</span></div><div class="line">[root@master heartbeat-3.0.4]<span class="comment"># cp authkeys ha.cf haresources /etc/ha.d</span></div><div class="line">[root@master heartbeat-3.0.4]<span class="comment"># cd /etc/ha.d</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后编辑</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master ha.d]<span class="comment"># vim authkeys</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;更改</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#auth 1</span></div><div class="line"><span class="comment">#1 crc</span></div><div class="line"><span class="comment">#2 shal HI!</span></div><div class="line"><span class="comment">#3 md5 Hello!</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改为</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#auth 3</span></div><div class="line"><span class="comment">#1 crc</span></div><div class="line"><span class="comment">#2 shal HI!</span></div><div class="line">3 md5 Hello!</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这是用来验证的 crc 最简单， shal 最复杂。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后修改权限</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master ha.d]<span class="comment"># chmod 600 authkeys</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;编辑 haresources 文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@master ha.d]<span class="comment"># vim haresources</span></div><div class="line"><span class="comment">#</span></div><div class="line"><span class="comment">#nodel 10.0.0.170 Filesystem::/dev/sda1::data1:;ext2</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;修改为</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="comment">#</span></div><div class="line">master 192.168.0.70/24/eth0:0 nginx</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;说明：master 为主节点 hostname ，192.168.0.70 为 vip ，/24 为24网段，eth0:0 为 vip 的设备名，nginx 为 heartbeat 监控的服务，也是两台机器对外提供的核心服务。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后编辑 ha.cf</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;清空 ha.cf</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@master ha.d]<span class="comment"># &gt;ha.cf</span></div><div class="line">[root@master ha.d]<span class="comment"># vim ha.cf</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;增加内容</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line">debugfile /var/<span class="built_in">log</span>/ha-debug</div><div class="line">logfile /var/<span class="built_in">log</span>/ha-log</div><div class="line">logfacility local0</div><div class="line">keepalive 2</div><div class="line">deadtime 30</div><div class="line">warntime 10</div><div class="line">initdead 60</div><div class="line">udpport 694</div><div class="line">ucast eth1 192.168.91.101</div><div class="line">auto_failback on</div><div class="line">node master</div><div class="line">node slave</div><div class="line">ping 192.168.91.1</div><div class="line">respawn hacluster /usr/lib64/heartbeat/ipfail</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置说明：</p>
<ul>
<li><code>debugfile /var/log/ha-debug</code>：该文件保存 heartbeat 的调试信息;</li>
<li><code>logfile /var/log/ha-log</code>：heartbeat 的日志文件；</li>
<li><code>keepalive 2</code>：心跳的时间间隔，默认时间单位为秒；</li>
<li><code>deadtime 30</code>：超出该时间间隔未收到对方节点的心跳，则认为对方已经死亡；</li>
<li><code>warntime 10</code>：超出该时间间隔未收到对方节点的心跳，则发出警告并记录到日志中；</li>
<li><code>initdead 60</code>：在某些系统上，系统启动或重启之后需要经过一段时间网络才能正常工作，该选项用于解决这种情况产生的时间间隔。取值至少为 deadtime 的两倍；</li>
<li><code>udpport 694</code>：设置广播通信使用的端口， 694 为默认使用的端口；</li>
<li><code>ucast eth1 192.168.91.101</code>：设置为对方机器心跳检测的网卡和 ip；</li>
<li><code>auto_failback on</code>：heartbeat 的两台机器分别为主节点和从节点。主节点在正常情况下占用资源并运行所有的服务，遇到故障时把资源交给从节点并由从节点运行服务。在该选项为 on 的情况下，一旦主节点恢复运行，则自动获取资源并取代从节点，负责不取代从节点；</li>
<li><code>node</code>： 指定主和从，各占一行，主在上从在下；</li>
<li><code>respawn hacluster /usr/lib/heartbeat/ipfail</code>：指定与 heartbeat 一同启动和关闭的进程，该进程被自动监视，遇到故障则重新启动。最常用的进程是 ipfail ，该进程用于检测和处理网络故障，需要配合 ping 语句指定的 ping node 来检测网络连接。如果系统是64，注意该文件的路径<code>/usr/lib64/heartbeat/ipfail</code>。</li>
</ul>
<h4 id="7-把主上的三个配置文件拷贝到从上"><a href="#7-把主上的三个配置文件拷贝到从上" class="headerlink" title="7.把主上的三个配置文件拷贝到从上"></a>7.把主上的三个配置文件拷贝到从上</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># cd /etc/ha.d/</span></div><div class="line">[root@master ha.d]<span class="comment"># scp authkeys ha.cf haresources slave:/etc/ha.d/</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;scp 命令安装</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">yum -y install openssh-clients</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;如果提示错误，主从都安装</p>
<h4 id="8-从上（slave）编辑-ha-cf"><a href="#8-从上（slave）编辑-ha-cf" class="headerlink" title="8.从上（slave）编辑 ha.cf"></a>8.从上（slave）编辑 ha.cf</h4><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@slave ~]<span class="comment"># vim /etc/ha.d/ha.cf</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;ucast eth1 192.168.91.101 修改为 ucast eth1 192.168.91.100</p>
<h4 id="9-启动-heartbeat"><a href="#9-启动-heartbeat" class="headerlink" title="9.启动 heartbeat"></a>9.启动 heartbeat</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;先主，后从</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line"><span class="comment"># service heartbeat start</span></div></pre></td></tr></table></figure>
<h4 id="10-检查测试"><a href="#10-检查测试" class="headerlink" title="10.检查测试"></a>10.检查测试</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># ifconfig</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;看是否有 eth0:0</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/heartbeat%E9%83%A8%E7%BD%B2%E8%BF%87%E7%A8%8B/02.png?raw=true" alt="images"><figcaption class="figure__caption">images</figcaption></figure></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># ps aux | grep nginx</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;看是否有 nginx 进程</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/heartbeat%E9%83%A8%E7%BD%B2%E8%BF%87%E7%A8%8B/03.png?raw=true" alt="images"><figcaption class="figure__caption">images</figcaption></figure></p>
<h4 id="11-测试1"><a href="#11-测试1" class="headerlink" title="11.测试1"></a>11.测试1</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主上故意禁 ping </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># iptables -I INPUT -p icmp -j DROP</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;从上执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@slave ~]<span class="comment"># ifconfig</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;看是否有 eth0:0</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/heartbeat%E9%83%A8%E7%BD%B2%E8%BF%87%E7%A8%8B/04.png?raw=true" alt="images"><figcaption class="figure__caption">images</figcaption></figure></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@slave ~]<span class="comment"># ps aux | grep nginx</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;看是否有 nginx 进程</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/heartbeat%E9%83%A8%E7%BD%B2%E8%BF%87%E7%A8%8B/05.png?raw=true" alt="images"><figcaption class="figure__caption">images</figcaption></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主上执行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># iptables -D INPUT -p icmp -j DROP</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主上恢复 eth0:0 和 nginx，从上停止 eth0:0 和 nginx 服务</p>
<h4 id="12-测试2"><a href="#12-测试2" class="headerlink" title="12.测试2"></a>12.测试2</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主上停止 heartbeat 服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># service heartbeat stop</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;从上会启动 eth0:0 和 nginx 服务</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主上开启 heartbeat 服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@master ~]<span class="comment"># service heartbeat start</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主上恢复 eth0:0 和 nginx 服务，从上停止 eth0:0 和 nginx 服务</p>
<h4 id="13-测试脑裂"><a href="#13-测试脑裂" class="headerlink" title="13.测试脑裂"></a>13.测试脑裂</h4><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主和从上都 down 掉 eth1 网卡</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">ifdown eth1</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;主和从都会启动 eth0:0 网卡和 nginx 服务</p>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Haproxy/5. haproxy 基础配置文件详解/">haproxy 基础配置文件详解</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/haproxy/">haproxy</a>
			</span>
		
	</div>

	

	
		<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/haproxy%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E8%AF%A6%E8%A7%A3/01.png?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>HAproxy可以实现基于TCP（四层 例如:SSH,SMTP,MYSQL）和HTTP（七层 例如:web服务器）应用的代理软件，同时也可以作为负载均衡器使用，并且是开源完全免费的。HAproxy完全可以支持数以万计的并发链接，它的工作模式可以将其简单而安全地整合到当前的服务架构中，同时可以保护你的WEB服务器不暴露到网络上（设置成代理来实现的 通过VIP将后端的web服务器隐藏到内网中）。</strong></p>
<h2 id="HAProxy有以下几个优点"><a href="#HAProxy有以下几个优点" class="headerlink" title="HAProxy有以下几个优点"></a>HAProxy有以下几个优点</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;1、免费开源，稳定性也非常好，其稳定性可以与硬件级别的F5 BIG-IP相媲美。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;2、负载带宽非常大。根据官方文档可知，HAproxy可以跑满10Gbps的带宽，对于软件级负载均衡器来说，是相当惊人的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;3、支持链接拒绝。因为保护一个链接保持打开状态的开销是很低的，有时我们需要防止蠕虫攻击，也就是通过限制它们的连接打开来防止它们的危害，可以用于防止DDoS攻击，这也是其他负载均衡器所不具备的。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;4、支持全透明代理（已具备硬件防火墙的典型特点）。可以用客户端IP地址或任何其他地址来链接后端服务器，这个特性仅在Linux 2.4/2.6 内核打了cttproxy补丁后才可以使用。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;5、支持TCP层（四层）的负载均衡。HAproxy现在多用于线上的MySQL集群环境，常用它作为MySQL（读）负载均衡，不过在后端的MySQL Slaves数量超过10台时性能不如LVS，所以更推荐推荐LVS+Keepalived。<strong>一般情况下都是前端通过LVS做四层负载 HAProxy做后端web服务器的负载 这样性能会比单独用HAProxy高很多</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;6、强大的监控服务。自带强大的监控服务器状态的页面，在生产环境中可结合Nagios来实现邮件或短信报警。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;7、能支持多种的负载均衡算法，现在为止可以支持8种</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;8、支持虚拟主机。</p>
<h2 id="haproxy-的配置文件（haproxy-conf）详解"><a href="#haproxy-的配置文件（haproxy-conf）详解" class="headerlink" title="haproxy 的配置文件（haproxy.conf）详解"></a>haproxy 的配置文件（haproxy.conf）详解</h2><h3 id="1-global"><a href="#1-global" class="headerlink" title="1.global"></a>1.global</h3><ul>
<li>local0日志设备，info日志级别，日志界别有：err warning  info  debug  4种。这个配置表示使用127.0.0.1上的rsyslog服务中的local0日志设备</li>
<li>maxconn 20480   最大并发数</li>
<li>daemon  后台模式</li>
<li>nbproc：进程数</li>
<li>user：需要些用户名</li>
<li>gid：需要写数字</li>
<li>pidfile：pid文件、</li>
</ul>
<h3 id="2-defaults"><a href="#2-defaults" class="headerlink" title="2.defaults"></a>2.defaults</h3><ul>
<li>mode：http    tcp模式，http模式和health模式（健康检查已经废弃）</li>
<li>retries：3   设置后端服务器的失败重试次数，如果连接失败的次数超过这里设置的值，haproxy就会对后端服务器标记不可用，也可以在后面进行设置</li>
<li>timeout connect 10s 设置成功连接到一台服务器的最长等待时间，默认单位是毫秒，也可以使用其他单位换算</li>
<li>timeout client 20s   设置成功连接客户端发动数据时最长等待时间，………………</li>
<li>imeout server 30s     设置服务器端回应客户端数据发送的最长等待时间…………</li>
<li>timeout check 5s      设置对后端服务器的检测超时时间，…………</li>
</ul>
<h3 id="3-frontend"><a href="#3-frontend" class="headerlink" title="3.frontend"></a>3.frontend</h3><ul>
<li>frontend关键字定义了一个名字为www的前端虚拟节点</li>
<li>bind格式：bind [<address>:<port>]  interface <interface>    interface可选项，用来指定网络接口的名称，只在linux上使用</interface></port></address></li>
<li>option httplog        默认情况下，haproxy日志是不记录http请求的，这样不方便haproxy的排错和监控，此项启动日志记录http请求</li>
<li>option forwardfor     后端服务器需要获得客户端的真实ip，就需要配置此参数</li>
<li>option httpclose:           此项表示客户端和服务器端完成一次连接请求后，haproxy将主动关闭tcp连接。这是对性能非常有帮助的参数</li>
<li>log global：     表示使用全局的日志配置，</li>
<li>default_backend：    指定默认的后端服务器池，也就是指一组后端真是服务器，而真是服务器组将在backend段定义。这里的htmpool就是一个后端服务器组。</li>
</ul>
<h3 id="4-backend"><a href="#4-backend" class="headerlink" title="4.backend"></a>4.backend</h3><ul>
<li>backend关键字定义了一个名为htmpool的后端真是服务器组。</li>
<li>mode  http   http模式</li>
<li>option  redispatch：此参数用于cookie保持的环境中。在默认的环境下，HAProxy会将其请求的后端服务器的serverID插入到cookie中，以保证会话的session持久性。而如果后端的服务器出现故障，客户端的cookie是不会刷新的，这就会出现问题。此时如果设置此参数，将会将客户端的请求强制定向到另外一台监控的后端服务器上，一保证服务器的正常。</li>
<li>option abortonclose：如果设置此参数，在服务器负载很高的情况下，自动结束当前队列处理时间比较长的连接</li>
<li>balance：此关键字用来定义负载均衡算法：常用的算法有：<ul>
<li>1.roundrobin：基于权重轮训    </li>
<li>2.static-rr：静态权重轮训，运行时调整其服务器权重不会生效</li>
<li>3.source：ip算法，ip_hash，同一个客户端的请求转发在特定的后端服务器上</li>
<li>4.leastconn：此算法会将新的连接请求转发到最少连接数目的后端服务器，在回话时间较长的环境中使用，比如：数据库负载均衡等，不适合会话短的环境</li>
<li>5.uri：此算法会对部分或者整个URL进行hash运算，在经过与服务器的总权重相除，最后转发到某台匹配的后端服务器上</li>
<li>6.uri_param：此算法会根据URL路劲中的参数进行转发，这样可保证在后端真是服务器数量不变时，同一个用户的请求始终分发到一台机器上</li>
<li>7.hdr：此算法根据http头进行转发，如果指定的http头名称不存在，则使用roundrobin算法进行策略转发。</li>
</ul>
</li>
<li>cookie：表示允许向cookie插入SERVERID，每台服务器的SERVERID可在下面的server关键字中使用cookie关键字定义</li>
<li>option httpchk：此选项表示启用http的服务状态监测功能。haproxy作为一个专业的负载均衡器，他支持对backend部分指定的后端服务器节点的健康检查，以保证在后端backend中某个节点不能服务时，吧从frotend段进来的客户端请求分配至backend中其他健康节点上，从而保证整理服务的可用性。option httpchk具体用法如下：</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">option httpchk &lt;mothod&gt; &lt;uri&gt; &lt;version&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;method:常用的请求的方法：OPTIONS, GET,HEAD，一般用head方式，head仅检查response的head是不是状态码200，head相对get更快，更简单<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;uri：表示要检测的url地址，200为正常，其他都为错误<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;version：指定心跳检测时的http版本号    </p>
<ul>
<li>server：定义后端真实服务器，不能用于default和frontend部分，使用格式：</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">server	&lt;name&gt;		&lt;address&gt;[:port]  [param*]</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;参数含义：</p>
<ul>
<li><p><name>：后端服务器指定一个内部名称，随便写</name></p>
</li>
<li><p>[param*]：常用参数：</p>
</li>
<li><p>check：表示启用对此后端服务器执行健康检查</p>
</li>
<li><p>inter：设置健康状态检查的时间间隔，单位毫秒</p>
</li>
<li><p>rise：社会中重故障状态转换正常状态的次数</p>
</li>
<li><p>fall：设置后端服务器从正常状态转换至不可用状态的检查次数：</p>
</li>
<li><p>cookie：指定后端服务器设定的cookie值。</p>
</li>
<li><p>weight：权重，值1-256，0代表不参与负载均衡</p>
</li>
<li><p>backup：设置后端服务器的备份服务器，仅仅在后端所有真是服务器均不可用的情况下次啊启用</p>
</li>
</ul>
<h3 id="5-listen"><a href="#5-listen" class="headerlink" title="5.listen"></a>5.listen</h3><ul>
<li>listen关键字定义了一个admin_stats的实例，</li>
<li>stats refresh：设置haproxy监控统计页面自动刷新的时间。</li>
<li>stats uri：设置haproxy监控统计页面的URL路劲，通过ip：port/haproxy-status查看，路劲自己随意设置</li>
<li>stats realm：设置登录haproxy统计页面的密码框上的文本信息</li>
<li>stats auth：设置登录haproxy统计页面的用户名和密码。可以设置多个，设置多行</li>
<li>stats hide-version：用来隐藏统计页面上haproxy的版本信息</li>
<li>stats admin if TURE：在1.4.9版本以后，可以在监控页面上手工启动或者禁用后端服务器</li>
</ul>

	

	

</article>




	<article>
	
		<h1><a href="/2017/10/12/Haproxy/6. 源码编译安装 haproxy/">haproxy 源码编译安装</a></h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/haproxy/">haproxy</a>
			</span>
		
	</div>

	

	
		<h2 id="下载"><a href="#下载" class="headerlink" title="下载"></a>下载</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line">[root@localhost ~]<span class="comment"># cd /usr/local/src</span></div><div class="line">[root@localhost src]<span class="comment"># wget http://www.haproxy.org/download/1.4/src/haproxy-1.4.25.tar.gz</span></div><div class="line">[root@localhost src]<span class="comment"># yum -y install cmake  gcc gcc-c++  autoconf automake zlib*  libxml* \  </span></div><div class="line">ncurses ncurses-devel libtool-ltdl-devel* make  bison bison-devel libaio   </div><div class="line">[root@localhost src]<span class="comment"># tar zxvf haproxy-1.4.25.tar.gz   </span></div><div class="line">[root@localhost src]<span class="comment"># cd haproxy-1.4.25  </span></div><div class="line">[root@localhost haproxy-1.4.25]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h2 id="开始编译安装"><a href="#开始编译安装" class="headerlink" title="开始编译安装"></a>开始编译安装</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;MAKE 参数参考文件中的 README 文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">[root@localhost haproxy-1.4.25]<span class="comment"># make TARGET=linux26 PREFIX=/usr/local/haproxy  </span></div><div class="line">[root@localhost haproxy-1.4.25]<span class="comment"># make install PREFIX=/usr/local/haproxy  </span></div><div class="line">[root@localhost haproxy-1.4.25]<span class="comment"># install -d /usr/local/sbin  </span></div><div class="line">[root@localhost haproxy-1.4.25]<span class="comment"># install haproxy /usr/local/sbin  </span></div><div class="line">[root@localhost haproxy-1.4.25]<span class="comment"># install -d /usr/local/share/man/man1  </span></div><div class="line">[root@localhost haproxy-1.4.25]<span class="comment"># install -m 644 doc/haproxy.1 /usr/local/share/man/man1  </span></div><div class="line">[root@localhost haproxy-1.4.25]<span class="comment"># install -d /usr/local/doc/haproxy  </span></div><div class="line">[root@localhost haproxy-1.4.25]<span class="comment"># for x in configuration architecture haproxy-en haproxy-fr; do \  </span></div><div class="line">&gt; install -m 644 doc/<span class="variable">$x</span>.txt /usr/<span class="built_in">local</span>/doc/haproxy ; \  </div><div class="line">&gt; <span class="keyword">done</span>  </div><div class="line">[root@localhost haproxy-1.4.25]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;安装完成，检测是否安装成功</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@localhost haproxy-1.4.25]<span class="comment"># haproxy -v  </span></div><div class="line">HA-Proxy version 1.4.25 2014/03/27  </div><div class="line">Copyright 2000-2014 Willy Tarreau &lt;w@1wt.eu&gt;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;则安装成功</p>
<h2 id="配置-haproxy-cfg-文件"><a href="#配置-haproxy-cfg-文件" class="headerlink" title="配置 haproxy.cfg 文件"></a>配置 haproxy.cfg 文件</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;用的 /root/haproxy-1.4.25/examples/haproxy.cfg   这个是自带的配置文件</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div></pre></td><td class="code"><pre><div class="line">[root@localhost examples]<span class="comment"># vim haproxy.cfg  </span></div><div class="line">  </div><div class="line"><span class="comment"># this config needs haproxy-1.1.28 or haproxy-1.2.1  </span></div><div class="line">  </div><div class="line">global  </div><div class="line">        <span class="built_in">log</span> 127.0.0.1   local0  </div><div class="line">        <span class="built_in">log</span> 127.0.0.1   local1 notice  </div><div class="line">        <span class="comment">#log loghost    local0 info  </span></div><div class="line">        maxconn 4096  </div><div class="line"><span class="comment">#       chroot /usr/share/haproxy  </span></div><div class="line">        chroot  /usr/<span class="built_in">local</span>/haproxy  </div><div class="line">        uid 99  </div><div class="line">        gid 99  </div><div class="line">        daemon  </div><div class="line">        <span class="comment">#debug  </span></div><div class="line">        <span class="comment">#quiet  </span></div><div class="line">  </div><div class="line">defaults  </div><div class="line">        <span class="built_in">log</span>     global  </div><div class="line">        mode    http  </div><div class="line">        option  httplog  </div><div class="line">        option  dontlognull  </div><div class="line">        retries 3  </div><div class="line">        <span class="comment">#redispatch  </span></div><div class="line">        maxconn 2000  </div><div class="line">        contimeout      5000  </div><div class="line">        clitimeout      50000  </div><div class="line">        srvtimeout      50000  </div><div class="line"><span class="comment">#============================这一段是后面加上去的。 就是WEB代理  </span></div><div class="line">listen web_proxy  </div><div class="line">        <span class="built_in">bind</span> *:80  </div><div class="line">        mode    http  </div><div class="line">        option httpchk GET /index.html  </div><div class="line">        server s1 192.168.11.210:80 weight 3 check  </div><div class="line">        server s2 192.168.11.211:80 weight 3 check  </div><div class="line"><span class="comment">#后面的我做的注释  </span></div><div class="line"><span class="comment">#listen appli1-rewrite 0.0.0.0:10001  </span></div><div class="line"><span class="comment">#       cookie  SERVERID rewrite  </span></div><div class="line"><span class="comment">#       balance roundrobin  </span></div><div class="line"><span class="comment">#       server  app1_1 192.168.34.23:8080 cookie app1inst1 check inter 2000 rise 2 fall 5  </span></div><div class="line"><span class="comment">#       server  app1_2 192.168.34.32:8080 cookie app1inst2 check inter 2000 rise 2 fall 5  </span></div><div class="line"><span class="comment">#       server  app1_3 192.168.34.27:8080 cookie app1inst3 check inter 2000 rise 2 fall 5  </span></div><div class="line"><span class="comment">#       server  app1_4 192.168.34.42:8080 cookie app1inst4 check inter 2000 rise 2 fall 5  </span></div><div class="line"><span class="comment">#  </span></div><div class="line"><span class="comment">#listen appli2-insert 0.0.0.0:10002  </span></div><div class="line"><span class="comment">#       option  httpchk  </span></div><div class="line"><span class="comment">#       balance roundrobin  </span></div><div class="line"><span class="comment">#       cookie  SERVERID insert indirect nocache  </span></div><div class="line"><span class="comment">#       server  inst1 192.168.114.56:80 cookie server01 check inter 2000 fall 3  </span></div><div class="line"><span class="comment">#       server  inst2 192.168.114.56:81 cookie server02 check inter 2000 fall 3  </span></div><div class="line"><span class="comment">#       capture cookie vgnvisitor= len 32  </span></div><div class="line"><span class="comment">#  </span></div><div class="line"><span class="comment">#       option  httpclose               # disable keep-alive  </span></div><div class="line"><span class="comment">#       rspidel ^Set-cookie:\ IP=       # do not let this cookie tell our internal IP address  </span></div><div class="line"><span class="comment">#         </span></div><div class="line"><span class="comment">#listen appli3-relais 0.0.0.0:10003  </span></div><div class="line"><span class="comment">#       dispatch 192.168.135.17:80  </span></div><div class="line"><span class="comment">#  </span></div><div class="line"><span class="comment">#listen appli4-backup 0.0.0.0:10004  </span></div><div class="line"><span class="comment">#       option  httpchk /index.html  </span></div><div class="line"><span class="comment">#       option  persist  </span></div><div class="line"><span class="comment">#       balance roundrobin  </span></div><div class="line"><span class="comment">#       server  inst1 192.168.114.56:80 check inter 2000 fall 3  </span></div><div class="line"><span class="comment">#       server  inst2 192.168.114.56:81 check inter 2000 fall 3 backup  </span></div><div class="line"><span class="comment">#  </span></div><div class="line"><span class="comment">#listen ssl-relay 0.0.0.0:8443  </span></div><div class="line"><span class="comment">#       option  ssl-hello-chk  </span></div><div class="line"><span class="comment">#       balance source  </span></div><div class="line"><span class="comment">#       server  inst1 192.168.110.56:443 check inter 2000 fall 3  </span></div><div class="line"><span class="comment">#       server  inst2 192.168.110.57:443 check inter 2000 fall 3  </span></div><div class="line"><span class="comment">#       server  back1 192.168.120.58:443 backup  </span></div><div class="line"><span class="comment">#listen appli5-backup 0.0.0.0:10005  </span></div><div class="line"><span class="comment">#       option  httpchk *  </span></div><div class="line"><span class="comment">#       balance roundrobin  </span></div><div class="line"><span class="comment">#       cookie  SERVERID insert indirect nocache  </span></div><div class="line"><span class="comment">#       server  inst1 192.168.114.56:80 cookie server01 check inter 2000 fall 3  </span></div><div class="line"><span class="comment">#       server  inst2 192.168.114.56:81 cookie server02 check inter 2000 fall 3  </span></div><div class="line"><span class="comment">#       server  inst3 192.168.114.57:80 backup check inter 2000 fall 3  </span></div><div class="line"><span class="comment">#       capture cookie ASPSESSION len 32  </span></div><div class="line"><span class="comment">#       srvtimeout      20000  </span></div><div class="line"><span class="comment">#       option  httpclose               # disable keep-alive  </span></div><div class="line"><span class="comment">#       option  checkcache              # block response if set-cookie &amp; cacheable  </span></div><div class="line"><span class="comment">#       rspidel ^Set-cookie:\ IP=       # do not let this cookie tell our internal IP address  </span></div><div class="line"><span class="comment">#       errorloc        502     http://192.168.114.58/error502.html  </span></div><div class="line"><span class="comment">#       errorfile       503     /etc/haproxy/errors/503.http</span></div></pre></td></tr></table></figure>
<h2 id="启动服务"><a href="#启动服务" class="headerlink" title="启动服务"></a>启动服务</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@localhost examples]<span class="comment"># haproxy -f /root/haproxy-1.4.25/examples/haproxy.cfg   </span></div><div class="line">[WARNING] 316/220055 (2376) : parsing [/root/haproxy-1.4.25/examples/haproxy.cfg:22]: keyword <span class="string">'redispatch'</span> is deprecated, please use <span class="string">'option redispatch'</span> instead.  </div><div class="line">[ALERT] 316/220055 (2376) : [haproxy.main()] Cannot chroot(/usr/<span class="built_in">local</span>/sbin/haproxy).</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;出现的报错信息，下面我们来解决问题。因为上面配置文件是我改后的，应该没有什么问题。<br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>[ALERT] 316/220055 (2376) : [haproxy.main()] Cannot chroot(/usr/local/sbin/haproxy).</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>是配置文件chroot的目录不对，根据我的文档安装改成 chroot /urs/local/haproxy 就可以解决了</code>  执行 mkdir /usr/share/haproxy</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>[WARNING] 316/220055 (2376) : parsing [/root/haproxy-1.4.25/examples/haproxy.cfg:22]: keyword &#39;redispatch&#39; is deprecated, please use &#39;option redispatch&#39; instead.</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<code>这个报错信息是配置文件22行的问题， 我是做了注释掉就可以起动服务了。   注释到相对应的错误行</code></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再次起动服务：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost examples]<span class="comment"># haproxy -f /root/haproxy-1.4.25/examples/haproxy.cfg   </span></div><div class="line">[root@localhost examples]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;没有任何提示，那我们检测一下是否启动成功</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;看一下进程信息</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@localhost examples]<span class="comment"># ps -ef |grep haproxy  </span></div><div class="line">nobody    3826     1  0 22:38 ?        00:00:00 haproxy -f /root/haproxy-1.4.25/examples/haproxy.cfg  </div><div class="line">root      3837  1260  0 22:38 pts/0    00:00:00 grep haproxy</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;进程已经在</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;看一下端口信息</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">[root@localhost examples]<span class="comment"># netstat -anp |grep haproxy  </span></div><div class="line">tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN      3826/haproxy          </div><div class="line">udp        0      0 0.0.0.0:43549               0.0.0.0:*                               3826/haproxy</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;把配置文件移动到/etc目录下</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost examples]<span class="comment"># cd /etc/  </span></div><div class="line">[root@localhost etc]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;复制haproxy配置文件到/etc/目录下面  </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[root@localhost etc]<span class="comment"># cp /root/haproxy-1.4.25/examples/haproxy.cfg haproxy.cfg</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再一次查看进程 kill掉。 在用/etc/haproxy.cfg的配置文件启动看看  </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">[root@localhost etc]<span class="comment"># ps -ef | grep haproxy  </span></div><div class="line">nobody    3826     1  0 22:38 ?        00:00:00 haproxy -f /root/haproxy-1.4.25/examples/haproxy.cfg  </div><div class="line">root      3901  1260  0 22:40 pts/0    00:00:00 grep haproxy  </div><div class="line">[root@localhost etc]<span class="comment"># kill 3826  </span></div><div class="line">[root@localhost etc]<span class="comment"># ps -ef | grep haproxy  </span></div><div class="line">root      3928  1260  0 22:41 pts/0    00:00:00 grep haproxy  </div><div class="line">```  </div><div class="line"></div><div class="line">&amp;<span class="comment">#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;已经kill掉进程，进程已经没有在运行了， 我们现在用/etc/haproxy.cfg 来运行服务  </span></div><div class="line"></div><div class="line">```bash</div><div class="line">[root@localhost etc]<span class="comment"># haproxy -f /etc/haproxy.cfg  </span></div><div class="line">[root@localhost etc]<span class="comment"># ps -ef |grep haproxy.cfg  </span></div><div class="line">nobody    3959     1  0 22:42 ?        00:00:00 haproxy -f /etc/haproxy.cfg  </div><div class="line">root      3965  1260  0 22:43 pts/0    00:00:00 grep haproxy.cfg</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;已经看到运行成功</p>
<h2 id="设置以服务形式启动"><a href="#设置以服务形式启动" class="headerlink" title="设置以服务形式启动"></a>设置以服务形式启动</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;目录切换到/etc/init.d</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div><div class="line">68</div><div class="line">69</div><div class="line">70</div><div class="line">71</div><div class="line">72</div><div class="line">73</div><div class="line">74</div><div class="line">75</div><div class="line">76</div><div class="line">77</div><div class="line">78</div><div class="line">79</div><div class="line">80</div><div class="line">81</div><div class="line">82</div><div class="line">83</div><div class="line">84</div><div class="line">85</div><div class="line">86</div><div class="line">87</div><div class="line">88</div><div class="line">89</div><div class="line">90</div><div class="line">91</div><div class="line">92</div><div class="line">93</div><div class="line">94</div><div class="line">95</div><div class="line">96</div><div class="line">97</div><div class="line">98</div></pre></td><td class="code"><pre><div class="line">[root@localhost etc]<span class="comment"># cd /etc/init.d/  </span></div><div class="line">[root@localhost init.d]<span class="comment"># vi haproxy  </span></div><div class="line">  </div><div class="line"><span class="meta">#!/bin/bash  </span></div><div class="line">    <span class="comment">#  </span></div><div class="line">    <span class="comment"># haproxy  </span></div><div class="line">    <span class="comment">#  </span></div><div class="line">    <span class="comment"># chkconfig: 35 85 15  </span></div><div class="line">    <span class="comment"># description: HAProxy is a free, very fast and reliable solution \  </span></div><div class="line">    <span class="comment"># offering high availability, load balancing, and \  </span></div><div class="line">    <span class="comment"># proxying for TCP and HTTP-based applications  </span></div><div class="line">    <span class="comment"># processname: haproxy  </span></div><div class="line">    <span class="comment"># config: /etc/haproxy.cfg  </span></div><div class="line">    <span class="comment"># pidfile: /var/run/haproxy.pid  </span></div><div class="line">    <span class="comment"># Source function library.  </span></div><div class="line">    . /etc/rc.d/init.d/<span class="built_in">functions</span>  </div><div class="line">    <span class="comment"># Source networking configuration.  </span></div><div class="line">    . /etc/sysconfig/network  </div><div class="line">    <span class="comment"># Check that networking is up.  </span></div><div class="line">    [ <span class="string">"<span class="variable">$NETWORKING</span>"</span> = <span class="string">"no"</span> ] &amp;&amp; <span class="built_in">exit</span> 0  </div><div class="line">    config=<span class="string">"/etc/haproxy.cfg"</span>  </div><div class="line">    <span class="built_in">exec</span>=<span class="string">"/usr/local/haproxy/sbin/haproxy"</span>  </div><div class="line">    prog=$(basename <span class="variable">$exec</span>)  </div><div class="line">    [ -e /etc/sysconfig/<span class="variable">$prog</span> ] &amp;&amp; . /etc/sysconfig/<span class="variable">$prog</span>  </div><div class="line">    lockfile=/var/lock/subsys/haproxy  </div><div class="line">    <span class="function"><span class="title">check</span></span>() &#123;  </div><div class="line">        <span class="variable">$exec</span> -c -V -f <span class="variable">$config</span>  </div><div class="line">    &#125;  </div><div class="line">    <span class="function"><span class="title">start</span></span>() &#123;  </div><div class="line">        <span class="variable">$exec</span> -c -q -f <span class="variable">$config</span>  </div><div class="line">        <span class="keyword">if</span> [ $? -ne 0 ]; <span class="keyword">then</span>  </div><div class="line">            <span class="built_in">echo</span> <span class="string">"Errors in configuration file, check with <span class="variable">$prog</span> check."</span>  </div><div class="line">            <span class="built_in">return</span> 1  </div><div class="line">        <span class="keyword">fi</span>  </div><div class="line">        <span class="built_in">echo</span> -n $<span class="string">"Starting <span class="variable">$prog</span>: "</span>  </div><div class="line">        <span class="comment"># start it up here, usually something like "daemon $exec"  </span></div><div class="line">        daemon <span class="variable">$exec</span> -D -f <span class="variable">$config</span> -p /var/run/<span class="variable">$prog</span>.pid  </div><div class="line">        retval=$?  </div><div class="line">        <span class="built_in">echo</span>  </div><div class="line">        [ <span class="variable">$retval</span> -eq 0 ] &amp;&amp; touch <span class="variable">$lockfile</span>  </div><div class="line">        <span class="built_in">return</span> <span class="variable">$retval</span>  </div><div class="line">    &#125;  </div><div class="line">    <span class="function"><span class="title">stop</span></span>() &#123;  </div><div class="line">        <span class="built_in">echo</span> -n $<span class="string">"Stopping <span class="variable">$prog</span>: "</span>  </div><div class="line">        <span class="comment"># stop it here, often "killproc $prog"  </span></div><div class="line">        killproc <span class="variable">$prog</span>  </div><div class="line">        retval=$?  </div><div class="line">        <span class="built_in">echo</span>  </div><div class="line">        [ <span class="variable">$retval</span> -eq 0 ] &amp;&amp; rm -f <span class="variable">$lockfile</span>  </div><div class="line">        <span class="built_in">return</span> <span class="variable">$retval</span>  </div><div class="line">    &#125;  </div><div class="line">    <span class="function"><span class="title">restart</span></span>() &#123;  </div><div class="line">        <span class="variable">$exec</span> -c -q -f <span class="variable">$config</span>  </div><div class="line">        <span class="keyword">if</span> [ $? -ne 0 ]; <span class="keyword">then</span>  </div><div class="line">            <span class="built_in">echo</span> <span class="string">"Errors in configuration file, check with <span class="variable">$prog</span> check."</span>  </div><div class="line">            <span class="built_in">return</span> 1  </div><div class="line">        <span class="keyword">fi</span>  </div><div class="line">        stop  </div><div class="line">        start  </div><div class="line">    &#125;  </div><div class="line">    <span class="function"><span class="title">reload</span></span>() &#123;  </div><div class="line">        <span class="variable">$exec</span> -c -q -f <span class="variable">$config</span>  </div><div class="line">        <span class="keyword">if</span> [ $? -ne 0 ]; <span class="keyword">then</span>  </div><div class="line">            <span class="built_in">echo</span> <span class="string">"Errors in configuration file, check with <span class="variable">$prog</span> check."</span>  </div><div class="line">            <span class="built_in">return</span> 1  </div><div class="line">        <span class="keyword">fi</span>  </div><div class="line">        <span class="built_in">echo</span> -n $<span class="string">"Reloading <span class="variable">$prog</span>: "</span>  </div><div class="line">        <span class="variable">$exec</span> -D -f <span class="variable">$config</span> -p /var/run/<span class="variable">$prog</span>.pid -sf $(cat /var/run/<span class="variable">$prog</span>.pid)  </div><div class="line">        retval=$?  </div><div class="line">        <span class="built_in">echo</span>  </div><div class="line">        <span class="built_in">return</span> <span class="variable">$retval</span>  </div><div class="line">    &#125;  </div><div class="line">    <span class="function"><span class="title">force_reload</span></span>() &#123;  </div><div class="line">        restart  </div><div class="line">    &#125;  </div><div class="line">    <span class="function"><span class="title">fdr_status</span></span>() &#123;  </div><div class="line">        status <span class="variable">$prog</span>  </div><div class="line">    &#125;  </div><div class="line">    <span class="keyword">case</span> <span class="string">"<span class="variable">$1</span>"</span> <span class="keyword">in</span>  </div><div class="line">        start|stop|restart|reload)  </div><div class="line">            <span class="variable">$1</span>  </div><div class="line">            ;;  </div><div class="line">        force-reload)  </div><div class="line">            force_reload  </div><div class="line">            ;;  </div><div class="line">        checkconfig)  </div><div class="line">            check  </div><div class="line">            ;;  </div><div class="line">        status)  </div><div class="line">            fdr_status  </div><div class="line">            ;;  </div><div class="line">        condrestart|try-restart)  </div><div class="line">          [ ! -f <span class="variable">$lockfile</span> ] || restart  </div><div class="line">        ;;  </div><div class="line">        *)  </div><div class="line">            <span class="built_in">echo</span> $<span class="string">"Usage: <span class="variable">$0</span> &#123;start|stop|status|checkconfig|restart|try-restart|reload|force-reload&#125;"</span>  </div><div class="line">            <span class="built_in">exit</span> 2  </div><div class="line">    <span class="keyword">esac</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重起服务haproxy</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">[root@localhost init.d]<span class="comment"># service haproxy restart  </span></div><div class="line">env: /etc/init.d/haproxy: Permission denied</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这个提示。大概是权限问题。下面加上执行权限  </p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">[root@localhost init.d]<span class="comment"># ll haproxy   </span></div><div class="line">-rw-r--r--. 1 root root 2588 Nov 13 23:02 haproxy  </div><div class="line">[root@localhost init.d]<span class="comment"># chmod -R 655 haproxy   </span></div><div class="line">[root@localhost init.d]<span class="comment"># ll haproxy   </span></div><div class="line">-rw-r-xr-x. 1 root root 2588 Nov 13 23:02 haproxy</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;看一下进程是否存在，不存在重起haproxy服务  </p>
<pre><code class="bash">[root@localhost init.d]<span class="comment"># ps -ef | grep haproxy  </span>
root      7409  1260  0 23:56 pts/0    00:00:00 grep haproxy  
[root@localhost init.d]<span class="comment"># service haproxy restart  </span>
Stopping haproxy:                                          [FAILED]  
Starting haproxy:                                          [  OK  ]  
[root@localhost init.d]<span class="comment"># ps -ef | grep haproxy  </span>
nobody    7444     1  0 23:56 ?        00:00:00 /usr/<span class="built_in">local</span>/haproxy/sbin/haproxy -D -f /etc/haproxy.cfg -p /var/run/haproxy.pid  
root      7449  1260  0 23:56 pts/0    00:00:00 grep haproxy  
[root@localhost init.d]<span class="comment">#</span>
</code></pre>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;配置上面就完成了  </p>

	

	

</article>





	<span class="different-posts">📖 <a href="/page/38">more posts</a> 📖</span>



	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
